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, MultiPointthepoints=[]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, 

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.