AsShape Opposite

592
4
08-09-2012 11:05 AM
KeithSandell
New Contributor III
Anyone know if there is a Geom to Json in python?

Essentially the opposite of arcpy.AsShape(), Json to Geometry object.
Tags (2)
0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
Geometry.__geo_interface__
0 Kudos
KeithSandell
New Contributor III
Okay, question:

Using the geo_interface on a multi-part feature with a hole in one of the parts returns the below value for coordinates.

If you notice in the midst of the coordinates is "None". The point at which this appears, appears to be the demarcation between the part and its hole.

How is "None" going to be interpreted by a program receiving the json?

[[(-9254388.2779676616, 3457813.8078284338), (-9377036.4553730804, 3457813.8078284338), (-9377036.4553730804, 3563951.6536600441), (-9254388.2779676616, 3563951.6536600441), (-9254388.2779676616, 3457813.8078284338)], [(-9406523.9989057705, 3626128.8613477843), (-9406523.9989057705, 3519991.0155161736), (-9529172.1763111949, 3519991.0155161736), (-9529172.1763111949, 3626128.8613477843), (-9406523.9989057705, 3626128.8613477843), None, (-9499902.5223645903, 3603858.4769027131), (-9500932.2585095931, 3540838.6248286134), (-9430498.3061914891, 3542486.2026606067), (-9430910.2006494887, 3603858.4769027131), (-9499902.5223645903, 3603858.4769027131)]]

This is how the Portal API returns the same geometry:

[[[-9254388,3457814],[-9377036,3457814],[-9377036,3563952],[-9254388,3563952],[-9254388,3457814]],[[-9406524,3626129],[-9406524,3519991],[-9529172,3519991],[-9529172,3626129],[-9406524,3626129]],[[-9499903,3603858],[-9500932,3540839],[-9430498,3542486],[-9430910,3603858],[-9499903,3603858]]]
0 Kudos
JasonScheirer
Occasional Contributor III
AsShape and __geo_interface__ are using GeoJSON, not Esri JSON. A few years ago it was a nascent standard that looked like it would take off so we adopted it, but it hasn't found much widespread use. We'll be adding Esri JSON support to geometries so you can feed them to REST APIs in 10.1 SP1. Until then, you'll need to do your own conversions.
0 Kudos
KeithSandell
New Contributor III
Thanks for the explanation. Good thing I came up with this then, I was just looking for an easier way to do it.

Obviously only for polygons...

class readGeo:

    def __init__(feat, geom):
        feat.geom = geom

    def returnGeom(feat):
        fGeom = feat.geom
        partCount = fGeom.partCount
        rings = "["
        featLst = []
        vrtxNum = 0
        while vrtxNum < partCount:
            part = fGeom.getPart(vrtxNum)
            pnt = part.next()
            while pnt:
                featLst.append("[" + str(int(round(pnt.X))) + ", " + str(int(round(pnt.Y))) + "]")
                pnt = part.next()
                if not pnt:
                    for pair in featLst:
                        rings += pair + ", "
                    del featLst[:]
                    rings = rings[0:len(rings)-2] + "]"
                    pnt = part.next()
                    if pnt:
                        rings += ", ["
                    elif partCount > 1:
                        rings += ", ["
            vrtxNum += 1
        if partCount == 1:
            return rings
        else:
            return rings[0:len(rings)-3]
0 Kudos