Skip to main content

Read and write spatial vectors

Project description

Read and write spatial vectors from shapefiles and CSVs thanks to GDAL and pandas.


sudo dnf -y install gdal-python3
# sudo apt-get -y install python3-gdal
virtualenv -p $(which python3) --system-site-packages \
source ~/.virtualenvs/crosscompute/bin/activate
pip install geotable


Load shapefiles.

In [1]: from geotable import GeoTable

In [2]: t = GeoTable.load('')

In [3]: t.iloc[0]
name                                                 b
quantity                                             2
cost                                              0.66
date                               1990-01-01 00:00:00
geometry_object         POINT (-91.5305465 14.8520705)
geometry_layer                                       b
geometry_proj4     +proj=longlat +datum=WGS84 +no_defs
Name: 0, dtype: object

Load CSVs containing spatial information.

GeoTable.load('wkt.csv')  # Load single CSV
GeoTable.load('')  # Load archive of multiple CSVs
GeoTable.load('', parse_dates=['date'])  # Configure pandas.read_csv

Handle CSVs with different geometry columns.

$ cat latitude_longitude.csv

$ cat lat_lon.csv

$ cat latitude_longitude_wkt.csv
a,1,0.33,1980-01-01,POINT(42.3736158 -71.10973349999999)

$ cat longitude_latitude_wkt.csv
a,1,0.33,1980-01-01,POINT(-71.10973349999999 42.3736158)

$ cat wkt.csv
aaa,1,0.33,1980-01-01,"POINT(-71.10973349999999 42.3736158)"
bbb,1,0.33,1980-01-01,"LINESTRING(-122.1374637 37.3796627,-92.5807231 37.1067189)"
ccc,1,0.33,1980-01-01,"POLYGON ((-83.10973350093332 42.37361082304877, -103.5305394806998 14.85206885307358, -95.7430260175515 42.28082607112266, -83.10973350093332 42.37361082304877))"

Handle CSVs with different spatial references.

$ cat proj4_from_file.csv
aaa,"POLYGON((326299 4693415,-1980130 1771892,-716771 4787516,326299 4693415))"

$ cat proj4_from_file.proj4
+proj=utm +zone=17 +ellps=WGS84 +datum=WGS84 +units=m +no_defs

$ cat proj4_from_row.csv
aaa,"LINESTRING(-122.1374637 37.3796627,-92.5807231 37.1067189)",l1,+proj=longlat +datum=WGS84 +no_defs
aaa,"POLYGON((326299 4693415,-1980130 1771892,-716771 4787516,326299 4693415))",l2,+proj=utm +zone=17 +ellps=WGS84 +datum=WGS84 +units=m +no_defs

Load and save in different spatial references.

from geotable.projections import SPHERICAL_MERCATOR_PROJ4
t = GeoTable.load('', target_proj4=SPHERICAL_MERCATOR_PROJ4)

from geotable.projections import LONGITUDE_LATITUDE_PROJ4
t.to_shp('/tmp/', target_proj4=LONGITUDE_LATITUDE_PROJ4)

Use LONGITUDE_LATITUDE_PROJ4 for compatibility with algorithms that use geodesic distance such as those found in geopy and pysal. Geodesic distance is also known as arc distance and is the distance between two points as measured using the curvature of the Earth. If your locations are spread over a large geographic extent, geodesic longitude and latitude coordinates provide greater accuracy than Euclidean XY coordinates.

from geotable.projections import LONGITUDE_LATITUDE_PROJ4
t = GeoTable.load('', target_proj4=LONGITUDE_LATITUDE_PROJ4)
t.to_csv('/tmp/', target_proj4=LONGITUDE_LATITUDE_PROJ4)
t.to_shp('/tmp/', target_proj4=LONGITUDE_LATITUDE_PROJ4)

Use the Universal Transverse Mercator (UTM) projection for compatibility with algorithms that use Euclidean distance on XY coordinates such as those found in scipy.spatial. If you know that your locations are confined to a small region, you can use the projected XY coordinates with standard Euclidean based algorithms, which tend to be significantly faster than their geodesic variants.

utm_proj4 = GeoTable.load_utm_proj4('')
t = GeoTable.load('', target_proj4=utm_proj4)
t.to_csv('/tmp/', target_proj4=utm_proj4)
t.to_shp('/tmp/', target_proj4=utm_proj4)

Use the Spherical Mercator projection when visualization is more important than accuracy. Do not use this projection for algorithms where spatial accuracy is important.

from geotable.projections import SPHERICAL_MERCATOR_PROJ4
t = GeoTable.load('wkt.csv', target_proj4=SPHERICAL_MERCATOR_PROJ4)
t.to_csv('/tmp/', target_proj4=SPHERICAL_MERCATOR_PROJ4)
t.to_shp('/tmp/', target_proj4=SPHERICAL_MERCATOR_PROJ4)

You can render your spatial vectors in Jupyter Notebook with the draw function. Each geometry layer will appear in a different color.

t = GeoTable.load('wkt.csv')
t.draw()  # Render the geometries in Jupyter Notebook


  • Add GeoTable.from_records
  • Add GeoTable.save_csv
  • Add GeoTable.save_shp
  • Reduce CSV size by omitting geometry_layer and geometry_proj4 unless needed


  • Add GeoTable.load
  • Add GeoTable.to_csv
  • Add GeoTable.to_shp
  • Add GeoTable.draw


  • Add ColorfulGeometryCollection

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for geotable, version
Filename, size File type Python version Upload date Hashes
Filename, size geotable- (11.1 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page