This post, on how to analyze an Esri shapefile with PostgreSQL, is adapted from material in my book Practical SQL: A Beginner’s Guide to Storytelling with Data from No Starch Press.
Spend some time digging into geographic information systems (GIS) and soon enough you’ll encounter a shapefile. It’s a GIS file type developed by mapping software firm Esri for use in its popular ArcGIS platform. A shapefile contains the geometric information to describe a shape—a river, road, lake, or town boundary, for example—plus metadata about the shape, such as its name.
Because the shapefile has become a de facto standard for publishing GIS data, other applications and software libraries use shapefiles too, such as the open source QGIS.
While researching GIS topics for a chapter in my book, Practical SQL, I learned that it’s easy to import a shapefile into a PostGIS-enabled PostgreSQL database. The information that describes each shape is stored in a column of data type
geometry, and so you can run spatial queries to calculate area, distances, intersections of objects, and more.
Here’s a quick exercise, adapted from the book.Continue…