Giter Site home page Giter Site logo

basemaps's Introduction

Basemaps in matplotlib/bokeh

Matplotlib's "basemap" ability allows the option of using shapefiles to add basemaps to your plots. When experimenting with this function, I ran into the following error:

ValueError: shapefile must have lat/lon vertices  - it looks like this one has vertices
in map projection coordinates. You can convert the shapefile to geographic
coordinates using the shpproj utility from the shapelib tools
(http://shapelib.maptools.org/shapelib-tools.html)

Some googling turned up a post stating that GDAL's ogr2ogr could handle these conversions, as opposed to figuring out how to use the shapelib tools.

The first requirement is to set up a working version of GDAL on your computer - I compiled this from source in Linux. Once I had GDAL working, I used ogr2ogr to convert my shapefile. ogr2ogr contains documentation on how to run it from the command line here. Alternatively, I experimented with the ogr2ogr Python module, taken from here. The Python module takes roughly the same syntax as the command line ogr2ogr, with individual arguments passed as a list.

The first shapefile I was working with was a shapefile of all the buildings in London, TQTL_LON.shp, downloaded from here. Looking at the original TQTL_LON.prj file, the shapefile is using the British National Grid projection - ogr2ogr is smart enough to figure this out on its own. ogr2ogr can also clip your plots to just a specific area by feeding it a bounding box of coordinates.

ogr2ogr.main(["", "-f", "ESRI Shapefile", 
                "-clipdst", "-0.237947", "51.449814", "0.007965", "51.553407",
                "Converted basemaps/London_buildings.shp", 
                "Raw basemaps/TQTL_LON.shp", 
                "-t_srs", "EPSG:4326"])

This code takes the original "TQTL_LON.shp" shapefile, clips it to a bounding box, and reprojects it into a new ESRI Shapefile called "London_buildings.shp", using the coordinate system EPSG:4326 (also known as WGS84).

I took a second shapefile, TQ_Roadlink.shp, from the Ordnance Survey website here, this time of all of the UK's roads. Running the above code on this shapefile however produced the following error:

ValueError: readshapefile can only handle 2D shape types

A simple fix - ogr2ogr can deal with this by forcing the coordinate dimensions (from XYZ to just XY).

ogr2ogr.main(["", "-f", "ESRI Shapefile",
                "-clipdst", "-0.237947", "51.449814", "0.007965", "51.553407",
                "Converted basemaps/London_roads.shp",
                "Raw basemaps/TQ_RoadLink.shp",
                "-t_srs", "EPSG:4326",
                "-dim", "2"])

Once I had converted both shapefiles, matplotlib could then easily plot both as basemaps (see ipynb for full code):

Buildings

Matplotlib buildings

Roads

Matplotlib roads

Bokeh

To add basemaps onto bokeh plots, I needed to convert the shapefiles into geoJSON format. Again, ogr2ogr can handle this:

ogr2ogr.main(["", "-f", "GeoJSON", 
                "-clipdst", "-0.237947", "51.449814", "0.007965", "51.553407",
                "Converted basemaps/London_buildings.geojson", 
                "Raw basemaps/TQTL_LON.shp", 
                "-t_srs", "EPSG:4326"])

Once the shapefiles are in a geoJSON format, I used bokeh's GeoJSONDataSource to add them both as "patches" to my bokeh plots (again, see ipynb file for full code):

Bokeh

basemaps's People

Contributors

charlie1347 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.