Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#leaflet #geojson
class GeoJsonUtil{
geojson2Leaflet(geojson , vectorConfig){
let shapes = [];
for(let i = 0, len = geojson.features.length; i < len; ++i){
let feature = geojson.features[i];
feature.id = feature.properties[vectorConfig.idField?vectorConfig.idField:'id']
feature.name = feature.properties[vectorConfig.nameField?vectorConfig.nameField:'name']
let geom = this._reverseLatLng(feature.geometry)
let shape = null
let opt = vectorConfig.style
opt.attr = feature.properties
opt.text = feature.name
let c = geom.coordinates;
if(geom.type.indexOf("Multi") !== -1){
c = geom.coordinates[0]
}
if(geom.type.indexOf("Point") !== -1) { // 点类型
opt.icon = this.L.icon({
iconUrl: vectorConfig.icon,
iconSize: vectorConfig.iconSize
})
shape = this.L.marker(c , opt)
}else if(geom.type.indexOf("LineString") !== -1) { // 线类型
shape = this.L.polyline(c, opt)
}else if(geom.type.indexOf("Polygon") !== -1) { // 面类型
shape = this.L.polygon(c, opt)
}
shapes.push(shape);
}
return shapes;
}
_reverseLatLng(geometry){
if(geometry.type.indexOf("Point") !== -1) { // 点类型
if(geometry.type.indexOf("Multi") !== -1){ // 2维
for(let i1 in geometry.coordinates){
geometry.coordinates[i1] = geometry.coordinates[i1].reverse()
}
}else { //1维
geometry.coordinates = geometry.coordinates.reverse()
}
}else if(geometry.type.indexOf("LineString") !== -1) { // 线类型
if(geometry.type.indexOf("Multi") !== -1){ // 3维
for(let i2 in geometry.coordinates){
for(let i1 in geometry.coordinates[i2]){
geometry.coordinates[i2][i1] = geometry.coordinates[i2][i1].reverse()
}
}
}else { // 2维
for(let i1 in geometry.coordinates){
geometry.coordinates[i1] = geometry.coordinates[i1].reverse()
}
}
}else if(geometry.type.indexOf("Polygon") !== -1) { // 面类型
if(geometry.type.indexOf("Multi") !== -1){ // 4维
for(let i3 in geometry.coordinates) {
for (let i2 in geometry.coordinates[i3]) {
for (let i1 in geometry.coordinates[i3][i2]) {
geometry.coordinates[i3][i2][i1] = geometry.coordinates[i3][i2][i1].reverse()
}
}
}
}else { // 3维
for(let i2 in geometry.coordinates){
for(let i1 in geometry.coordinates[i2]){
geometry.coordinates[i2][i1] = geometry.coordinates[i2][i1].reverse()
}
}
}
}
return geometry;
}
}
export default new GeoJsonUtil();
reverseLatLng(geometry){
if(geometry.type.indexOf("Point") !== -1) { // 点类型
if(geometry.type.indexOf("Multi") !== -1){ // 2维
for(let i1 in geometry.coordinates){
geometry.coordinates[i1] = geometry.coordinates[i1].reverse()
}
}else { //1维
geometry.coordinates = geometry.coordinates.reverse()
}
}else if(geometry.type.indexOf("LineString") !== -1) { // 线类型
if(geometry.type.indexOf("Multi") !== -1){ // 3维
for(let i2 in geometry.coordinates){
for(let i1 in geometry.coordinates[i2]){
geometry.coordinates[i2][i1] = geometry.coordinates[i2][i1].reverse()
}
}
}else { // 2维
for(let i1 in geometry.coordinates){
geometry.coordinates[i1] = geometry.coordinates[i1].reverse()
}
}
}else if(geometry.type.indexOf("Polygon") !== -1) { // 面类型
if(geometry.type.indexOf("Multi") !== -1){ // 4维
for(let i3 in geometry.coordinates) {
for (let i2 in geometry.coordinates[i3]) {
for (let i1 in geometry.coordinates[i3][i2]) {
geometry.coordinates[i3][i2][i1] = geometry.coordinates[i3][i2][i1].reverse()
}
}
}
}else { // 3维
for(let i2 in geometry.coordinates){
for(let i1 in geometry.coordinates[i2]){
geometry.coordinates[i2][i1] = geometry.coordinates[i2][i1].reverse()
}
}
}
}
return geometry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment