Convert Shape File to GeoJSON and Add to Page
Download this zip archive of 1940 Oakland Census Areas shape files. |
In QGIS, examine your layer's shape file to determine its CRS. Make a note of the EPSG number. Zip up (compress) your .shp, .dbf, and .shx (.prj optional) into a single .zip file Go to Ogre, a web service that translates spatial files into GeoJSON using the ogr2ogr tool, upload the zip file, and convert. |
The GeoJSON will look something like this
Save the result as filename.geojson. The conversion saves all the features in the shapefile as a single GeoJSON object of type "FeatureCollection." |
Note: The GeoJSON file will likely be very large. In practice we will probably keep it in a separate file and use a bit of Leaflet/Javascript to load it into a variable in our script. For this example, though, we'll just put it right into the code. We will assign this object to a variable (here, x):
We will use Leaflet's geoJson class to display the layer on the map. The generic syntax will look like this:
and we'll append the addTo(mapobject) method we have seen before.
We'll make a few other tweaks for display on this page but you can keep the original values. The <div> size has been reduced to 300x300 and the zoom changed to 10. |
References
- GitHub calvinmetcalf / leaflet.shapefile. See also his "Projected Shapefiles in Leaflet" using Stamen water color tiles.
- Vallandingham, J. From Shapefile to GeoJSON in which the author uses QGIS and GDAL to convert census tract shapefiles into GEOJSON.
- Vance, Steve. 2012. Converting shapefiles to GeoJSON, and other format conversions describes how to use MyGeoConverter.
- digital-geography.com 2013. Convert shapefiles to geoJSON via web interface