将ArcGIS Server的JSON转化为SHP文件

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# JSONToSHP.py
# Created on: 2014-09-01 22:00:00.00000
#   
# Description: 
# ---------------------------------------------------------------------------


import os
import json
import arcpy


jsonFile='E:/New Text Document.txt'
arcpy.env.workspace = "E:/output"
outpath = "E:/output"
outname = os.path.split(jsonFile)[1].split('.')[0] + ".shp"
print outname
fileObject = open(jsonFile, 'r')
jsonText = fileObject.read()
fileObject.close()
featureSet = json.loads(jsonText)
geometryType = featureSet["geometryType"]

#
geometry_type = "POINT"
if geometryType == "esriGeometryPolygon":
    geometry_type = "POLYGON"
elif geometryType == "esriGeometryPolyline":
    geometry_type = "POLYLINE"
elif geometryType == "esriGeometryPoint":
    geometry_type = "POINT"
elif geometryType == "esriGeometryMultiPoint":
    geometry_type = "MULTIPOINT"
    
# Execute CreateFeatureclass
arcpy.CreateFeatureclass_management(outpath, outname, geometry_type)
#
featureShape = outpath + "/" + outname
fields = featureSet["fields"]
fieldValid = [] #原属性名称
fieldValidLayer = ["SHAPE@"]  #新属性名称
for field in fields:
    fieldName = field["name"]
    fieldType = field["type"]
    if fieldName.lower() != 'objectid' and fieldName.lower() != 'shape_length' and fieldName.lower() != 'shape_area' and fieldName.lower() != 'fid':
        fieldValid.append(fieldName)
        if len(fieldName)>10:
            fieldName = fieldName[0:9]
        fieldValidLayer.append(fieldName)
        if fieldType=="esriFieldTypeInteger":
            arcpy.AddField_management(featureShape, fieldName, "LONG")
        elif fieldType=="esriFieldTypeSmallInteger":
            arcpy.AddField_management(featureShape, fieldName, "LONG")
        elif fieldType=="esriFieldTypeDouble":
            arcpy.AddField_management(featureShape, fieldName, "DOUBLE")        
        elif fieldType=="esriFieldTypeSingle":
            arcpy.AddField_management(featureShape, fieldName, "DOUBLE")     
        else:
            arcpy.AddField_management(featureShape, fieldName, "TEXT", 50)
#
features = featureSet["features"]
cur = arcpy.da.InsertCursor(featureShape, fieldValidLayer)
for feature in features:    
    attributes = feature["attributes"]
    geometry = feature["geometry"]
    valueList = []
    geojson = {}
    if geometryType == "esriGeometryPolygon":
        rings = geometry["rings"]
        geojson = {
            "type": "Polygon",
            "coordinates": rings}
    elif geometryType == "esriGeometryPolyline":
        paths = geometry["paths"]
        geojson = {
            "type": "MultiLineString",
            "coordinates": paths}
    elif geometryType == "esriGeometryPoint":
        geojson = {
            "type": "Point", 
            "coordinates": [geometry["x"], geometry["y"]]}
        
    polygon = arcpy.AsShape(geojson)
    valueList.append(polygon)  # (arcpy.Polygon(rings))
        
    for field in fieldValid:
        valueList.append(attributes[field])
    cur.insertRow(valueList)

del cur

print "finish !"
posted @ 2016-08-18 13:54  ParamousGIS  阅读(1575)  评论(0编辑  收藏  举报