Shapely

Shapely can be installed using:

pip install shapely

Or with conda:

conda install -c scitools shapely

Shapely makes the task of creating and working with geometries easier and makes your code cleaner. In the previous code, you concatenated a string to create a WKT representation of a point. Using Shapely, you can create a point and then convert it to WKT. The following code shows you how:

from shapely.geometry import Point, MultiPoint

thepoints=[]

for a in data["features"]:
code=a["attributes"]["ART_CODE"]
p=Point(float(a["geometry"]["x"]),float(a["geometry"]["y"]))
thepoints.append(p)
if a["geometry"]["x"]=='NaN':
pass
else:
cursor.execute("INSERT INTO art_pieces (code, location) VALUES ('{}',
ST_GeomFromText('{}'))".format(code, p.wkt))
connection.commit()

The previous code imports Point and MultiPoint from shapely.geometry. The code is the same as the previous version until the line in bold. To create a point, you use Point(x,y) in Shapely. It put all of the points in an array called thepoints to draw them in a Jupyter Notebook, for which an image is follows. Lastly, the SQL statement passes p.wkt to ST_GeomFromText().

In a Jupyter Notebook, you can print Shapely geometry just by typing the name of the variable holding the geometry and it will draw the end. The public art points are in the variable thepoints. A MultiPoint can be created using an array of points, and printing them draws the following image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset