arcgis json 与 geojson的互转
<script type="text/javascript"> //polygon、mutlipolygon var arcgisjson = { "displayFieldName": "", "fieldAliases": { "FID": "FID", "adcode": "adcode", "name": "name", }, "geometryType": "esriGeometryPolygon", "spatialReference": { "wkid": 4326, "latestWkid": 4326 }, "fields": [{ "name": "FID", "type": "esriFieldTypeOID", "alias": "FID" }, { "name": "adcode", "type": "esriFieldTypeDouble", "alias": "adcode" }, { "name": "name", "type": "esriFieldTypeString", "alias": "name", "length": 254 }, ], "features": [ { "attributes": { "FID": 0, "adcode": 440303, "name": "罗湖区", }, "geometry": { "rings": [ [ [ 114.10517299999999, 22.531628000000001 ], [ 114.104805, 22.532513000000002 ], [ 114.104586, 22.546033000000001 ], [ 114.10427900000001, 22.555434999999999 ], [ 114.10517299999999, 22.531628000000001 ], ], ] } }, { "attributes": { "FID" : 4, "adcode" : 440307, "name" : "龙岗区", }, "geometry": { "rings": [ [ [ 114.355782, 22.765794 ], [ 114.353458, 22.765993999999999 ], [ 114.35217799999999, 22.768257999999999 ], [ 114.355782, 22.765794 ] ], [ [ 114.339294, 22.623358 ], [ 114.339899, 22.625447000000001 ], [ 114.34240800000001, 22.626605999999999 ], [ 114.339294, 22.623358 ] ], ] } } ] } let tempgeojson = Arcgis_jsonToGeojson(arcgisjson) console.log(JSON.stringify(tempgeojson)); let temparcgisjson = GeojsonToArcgis_json(tempgeojson) console.log(JSON.stringify(temparcgisjson)); function Arcgis_jsonToGeojson(arcgis_json){ let geojson = { "type": "FeatureCollection", "features":null } let features = [] arcgis_json.features.forEach(item=>{ let feature = { "type": "Feature", "properties": item.attributes, "geometry": { "type": "MultiPolygon", "coordinates":null, } } if(item.geometry.rings.length>1){ //MultiPolygon feature.geometry.coordinates = item.geometry.rings.map(ring => [ring]); // let rings = [] // item.geometry.rings.forEach(ring=>{ // rings.push([ring]) // }) // feature.geometry.coordinates = rings }else{ //polygon feature.geometry.coordinates = [item.geometry.rings] } features.push(feature) }) geojson.features = features return geojson } function GeojsonToArcgis_json(geo_json){ let arcgis_json = { "displayFieldName": "", "fieldAliases": {}, "geometryType": "esriGeometryPolygon", "spatialReference": { "wkid": 4326, "latestWkid": 4326 }, "fields":[], "features":[] } let fieldAliases = {} let esriFieldTypeOID = null let fields = [] for(var key in geo_json.features[0].properties){ if(key == "FID" || key=="OBJECTID"){ if(key=="FID"){ esriFieldTypeOID = "FID" }else if(key=="OBJECTID" && !esriFieldTypeOID){ esriFieldTypeOID = "OBJECTID" } }else{ let fieldType = typeof geo_json.features[0].properties[key] === "number" ? "esriFieldTypeDouble" : "esriFieldTypeString"; let field = { "name": key, "type": fieldType, "alias": key }; if (fieldType === "esriFieldTypeString") { field.length = 254; // 默认字符串长度 } fields.push(field); } fieldAliases[key] = key } fields.unshift({ "name": esriFieldTypeOID, "type": "esriFieldTypeOID", "alias": esriFieldTypeOID }) arcgis_json.fieldAliases = fieldAliases arcgis_json.fields = fields //features let features = [] geo_json.features.forEach(item=>{ let feature = { "attributes":item.properties, "geometry":{ "rings":null } } if(item.geometry.coordinates.length>1){ //MultiPolygon feature.geometry.rings = item.geometry.coordinates.map(ring => ring[0]); // let rings = [] // item.geometry.coordinates.forEach(ring=>{ // rings.push(ring[0]) // }) // feature.geometry.rings = rings } else{ //polygon feature.geometry.rings = item.geometry.coordinates[0] } features.push(feature) }) arcgis_json.features = features return arcgis_json } </script>