Thursday, May 24, 2012

Feature Class as JSON (ArcPy 10.1)

I just learned a really easy way to display a feature class as JSON by using featureset object and the Describe().

fc = r"c:\temp\myData.shp"
featurSet = arcpy.FeatureSet()
featureSet.load(fc)
desc = arcpy.Describe(featureSet)
print desc.pjson # so we can read it
#### desc.json also works. ####
del desc
del fc
del featureSet

Output


{
  "displayFieldName" : "",
  "geometryType" : "esriGeometryPolygon",
  "spatialReference" : {
    "wkid" : 4326,
    "latestWkid" : 4326
  },
  "fields" : [
    {
      "name" : "FID",
      "type" : "esriFieldTypeOID",
      "alias" : "FID"
    },
    {
      "name" : "Id",
      "type" : "esriFieldTypeInteger",
      "alias" : "Id"
    }
  ],
  "features" : [
    {
      "attributes" : {
        "FID" : 0,
        "Id" : 0
      },
      "geometry" : {
        "rings" : [
          [
            [
              -170.48758544651321,
              27.863404726470606
            ],
            [
              -167.28669772639196,
              27.956184080677076
            ],
            [
              -167.28669772639196,
              25.497531194206999
            ],
            [
              -170.53397512361644,
              25.327435711495355
            ],
            [
              -170.48758544651321,
              27.863404726470606
            ]
          ],
          [
            [
              -169.37423319603627,
              27.229412472726722
            ],
            [
              -169.28145384182986,
              25.961427965239068
            ],
            [
              -167.90522675443472,
              26.208839576456285
            ],
            [
              -167.93615320583692,
              27.337655052634318
            ],
            [
              -169.37423319603627,
              27.229412472726722
            ]
          ]
        ]
      }
    }
  ]
}


Enjoy

6 comments:

Anonymous said...

What's the point of deleting your variables at the end?

None, that's what.

Andrew said...

I always clean up my variables when they are finished. It's just a coder's habit.

Anonymous said...

Found your blog recently when looking for converting to JSON. However, the code sample you provided doesn't actually work since there's no pjson method for Describe. What are you importing in the python script?

Ing. Domenico Ciavarella said...

Now with 10.1 sp1 you also can use SHAPE@JSON, SHAPE@WKB, and SHAPE@WKT tokens


fc = "c:/data/base.gdb/well"

with arcpy.da.SearchCursor(fc, ("OID@", "SHAPE@JSON")) as cursor:
for row in cursor:
print("Feature {0} has an area of {1}".format(row[0], row[1]))

Ing. Domenico Ciavarella said...

Now you can use
SHAPE@JSON, SHAPE@WKB, and SHAPE@WKT tokens. It were made available at ArcGIS 10.1 Service Pack 1.

fc = "c:/data/base.gdb/well"

with arcpy.da.SearchCursor(fc, ("OID@", "SHAPE@JSON")) as cursor:
for row in cursor:
print("Feature {0} has an area of {1}".format(row[0], row[1]))

Anonymous said...

Hi Andrew,

Great post. Could you also describe how one might go from JSON to feature class? I tried the
arcpy.JSONToFeatures_conversion but couldn't get it to work. (I keep getting ERROR 001558: Error parsing json file). Is there an alternative method that you recommend? Thanks a lot!

PS: Excellent blog. Keep going!