Code Snippets

The following code snippets provide quick tutorials on specific tools, datasets, or concepts. The snippets are written in Python using a Jupyter Notebook hosted on a cloud environment provided by Google Colab. A Google account is required to connect and run these snippets.

Esri Grid (ARC/INFO)
While the Esri ASCII grid file is in plain text format, the ARC/INFO GRID file format is more complicated. This snippet downloads elevation data stored in Esri grid and demonstrates how to extract the data using both rasterio and gdal Python packages.
Esri Shapefile
Shapefiles are a ubiquitous data format for storing vector (point, line and polygon) data. This tutorial shows you how to extract data from a shapefile and save it in GeoJSON format.
Geocode
Occasionally, your data consists of addresses or location descriptions rather than geographic coordinates. Luckily, there is a process that translates one into the other. Remember that translations inherently have some uncertainty associated with them; so, when it comes to accuracy, consider what your application actually requires (i.e., how close is close enough?). This tutorial demonstrates one method of using the geopy package to backend into the ArcGIS World Geocoding service.
GeoJSON
The quintessential file format for sharing data over the web may just be the JavaScript Object Notation (JSON). An extension of the JSON format to account for spatial geometries is the GeoJSON. This snippet looks at building a GeoJSON using built-in Python datatypes and a peak at how geopandas (an extension to the pandas datatypes) can write DataFrames to GeoJSON in a single line of code.
GeoTiff
GeoTIFF is based on TIFF (Tagged Image File Format) and is used as an interchange format for georeferenced raster imagery. The GeoTIFF file format is in widespread use worldwide and there is strong software support in both the open source and commercial GIS and spatial data analysis software products. This tutorial shows you how to read GeoTIFF raster files using both gdal and rasterio Python packages.
Plotting with matplotlib.pyplot
Finding data is one things, but how do you visualize it? This snippet shows you the first steps on how to read and ASCII raster file using numpy and rasterio and visualize it with matplotlib.pyplot.
Regex
Regex, or regular expressions, are algorithms that utilize a special syntax to characterize patterns within plain text. When it comes to web scraping, regex is one of the best tools in a data scientist’s toolbelt. This tutorial shows you some examples of regular expressions from scrubbed Wikipedia pages using the re Python package.
Reverse Geocode
Occasionally, you have coordinates but you are not certain where in the world they are located. One example of this is reading GPS coordinates from digital photographs taken with active location services. The result is a longitude and latitude pair stored at the approximate location of the photographer, but what’s the address? Reverse geocoding takes a coordinate pair and searches for the approximate address. This can be as fine scaled as a street number, or, more coarsely, at the city, state, or national level.
Table Join
Table joins are a practical method for combining tabular data to a spatial data set. Oftentimes this requires some data engineering to get the required table fields to match, but the added enhancement to your spatial data is worth the effort. This tutorial shows you how to do a simple table join between a CSV and a feature collection of polygons in GeoJSON.