Viewing Geometry in your Python IDE

1674
0
01-21-2020 05:00 AM
Labels (1)
DanPatterson_Retired
MVP Emeritus
4 0 1,674

Geometry ...

Previously...

/blogs/dan_patterson/2019/11/18/arcpy-shapes-viewing-in-spyder 

I really think it is a bit of overkill to create a FeatureClass just to see some geometry I have created or changed.

As of ArcGIS Pro 2.5, you can view a geometry object inside of Jupyter notebooks, Jupyter-lab or any other thing that supports SVG.  This missive shows how to view geometry from arcpy and also how to deal with geometry without having to go to arcpy to do it.

If you don't create or edit geometry with python, you can go now.

                                        

Start with geometry

Begin with some polygons.

In [ 1]: polys
In [or]: print(polys)
In [or]: polys.__repr__()

Out[1]:
[<Polygon object at 0x25941289d30[0x2594152f288]>,<Polygon object at 0x25941289cf8[0x259414a4bc0]>,
<Polygon object at 0x25941289c88[0x259414a4b98]>,<Polygon object at 0x25941289cc0[0x25940a241c0]>,
<Polygon object at 0x25941289c18[0x25940a240d0]>]

As In [ 1]: shows, you can get a 'representation' of the python geometry from within python.  It tells you that it is a polygon and then gives you the memory stuff (I presume).  The former you probably already knew and the latter you probably don't care about.  Pretty useless. So the quest continues.

Every object in python has a string representation, so lets try there.

In [ 2]: str(p_0)
In [or]: p_0.__str__()

Out[2]:
'<geoprocessing describe geometry object object at 0x000002594152F288>'

Heart be still! In [ 2]: is even more cryptic, but the memory location idea was spot on.

Let's see what 'dir' reveals.  I have snipped out a lot of stuff, and highlighted the more useful formats.  Make sure you explore the various geometry classes on your own.


In [3]: dir(p_0)

Out[3]:
['JSON', 'WKB', 'WKT', ... snip ...'__geo_interface__',
'__getSVG__', ... the new addition ....snip ...
'_fromGeoJson', ... snip ... '_repr_svg_', ... snip ...
...all the other properties and methods
]

Interesting.. maybe if a geometry contains more than one thing, slicing might real more.

In [4]: p_0[0]

Out[4]:
<Array
    [<Point (300010.0, 5000010.0, #, #)>, <Point (300010.0, 5000000.0, #, #)>,
    ... snip ...
    None,
          ... snip ...
    <Point (300002.0, 5000008.0, #, #)>, <Point (300001.0, 5000009.0, #, #)>,
    <Point (300001.0, 5000008.0, #, #)>, <Point (300002.0, 5000008.0, #, #)>]>

Perfect!  Polygons are made up of Array objects which are made up of Point objects.  Parts of arrays are separated by None, so you can have the polygons with multiple parts and/or holes in the parts.

Your education has been confirmed.

SVG

What is that __getSVG__ thing?

In [5]: p_0.__getSVG__()

Out[5]:
'<path fill-rule="evenodd"
fill="#66cc99"
stroke="#555555"
stroke-width="2.0"
opacity="0.6"
d=" M 300010,5000010 L 300010,5000000 L 300001.5,5000001.5 L 300000,5000010 L 300010,5000010
    M 300003,5000009 L 300003,5000003 L 300009,5000003 L 300009,5000009 L 300003,5000009
    M 300002,5000007 L 300001,5000007 L 300002,5000005 L 300002,5000007
    M 300002,5000008 L 300001,5000009 L 300001,5000008 L 300002,5000008
z"
/>'

Well they sure look like coordinates.  Time to go off and research SVG construction.

since __repr__ was revealing perhaps _repr_svg_ will be too.

In [6]: p_0._repr_svg_()

Out[6]:
'<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="100.0" height="100.0"
viewBox="299999.6 4999999.6 10.800000000046566 10.800000000745058"
preserveAspectRatio="xMinYMin meet">
<g transform="matrix(1,0,0,-1,0,10000010.0)">
<path fill-rule="evenodd"
fill="#66cc99"
stroke="#555555"
stroke-width="0.21600000001490116"
opacity="0.6"
d=" M 300010,5000010 L 300010,5000000 L 300001.5,5000001.5 L 300000,5000010 L 300010,5000010
    M 300003,5000009 L 300003,5000003 L 300009,5000003 L 300009,5000009 L 300003,5000009
    M 300002,5000007 L 300001,5000007 L 300002,5000005 L 300002,5000007
    M 300002,5000008 L 300001,5000009 L 300001,5000008 L 300002,5000008
z"
/>
</g></svg>'

                                        

On to the Display


 Inside of Spyder, which uses an IPython console,

I used my handy function for representing a numpy-based array as geometry... which I was working with. 

You get a quick peek at what it looks like. 

The alternative???

  •  use my other handy functions and make a featureclass,
  • crank up Pro and
  • add it to a map

Saving the SVG

Pretty easy.  Just right-click on it and you can save the SVG as an image or a *.svg file for use in such programs as Word.

The Code

I put it in a gist at...

 svg display for numpy geometries

If you are interested in alternatives and/or supplements to the various geometry packages, I have been working on

 ... npGeom ... which provides the basis for my

... Free Tools ...collection of tools normally restricted to the Advanced or Standard license for ArcGIS Pro.

So if you like geometry, between arcpy, python, numpy and the various display environments, you can find a match for the job at hand.

Try your hand with a triangle and a square...

t = np.array([[ 0.00,  0.00], [ 0.50,  1.00], [ 1.00,  0.00], [ 0.00,  0.00]])
r = np.array([[ 0.00,  2.00], [ 0.00,  1.00], [ 1.00,  1.00],
              [ 1.00,  2.00], [ 0.00,  2.00]])
About the Author
Retired Geomatics Instructor at Carleton University. I am a forum MVP and Moderator. Current interests focus on python-based integration in GIS. See... Py... blog, my GeoNet blog...
Labels