Skip to content

Instantly share code, notes, and snippets.

@csessig86
Last active January 2, 2016 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csessig86/8286932 to your computer and use it in GitHub Desktop.
Save csessig86/8286932 to your computer and use it in GitHub Desktop.
How I turned a csv into a binified shapefile and then a JSON file. Uses ogr2ogr (http://www.gdal.org/ogr2ogr.html) and binify (https://github.com/kevinschaul/binify)
# We start with a CSV called "crime_master_wloo_violent.csv" that has violent crime reports in Waterloo.
# Included in csv are columns for latitude ("lat") and longitude ("long"):
https://github.com/csessig86/crime_map2013/blob/master/csv/crime_master_wloo_violent.csv
# Command to turn CSV into DBF with the same name
ogr2ogr -f "ESRI Shapefile" crime_master_wloo_violent.dbf crime_master_wloo_violent.csv
# Create a file called "crime_master_wloo_violent.vrt"
# Change "SrcLayer" to the name of the source
# Change OGRVRTLayer name to "wloo_violent"
# Add name of lat, long columns to "GeometryField"
<OGRVRTDataSource>
<OGRVRTLayer name="wloo_violent">
<SrcDataSource relativeToVRT="1">./</SrcDataSource>
<SrcLayer>crime_master_wloo_violent</SrcLayer>
<GeometryType>wkbPoint</GeometryType>
<LayerSRS>WGS84</LayerSRS>
<GeometryField encoding="PointFromColumns" x="long" y="lat"/>
</OGRVRTLayer>
</OGRVRTDataSource>
# Create "violent" folder to stash shp file
mkdir violent
# Convert "vrt" file into shp and put it in our "violent" folder
ogr2ogr -f "ESRI Shapefile" violent crime_master_wloo_property.vrt
# Binify the "wloo_violent" shp into a shp called "wloo_violent_binify" with three options
# The first sets the number of hexagons across to 45
# The second sets the custom extent to a box around Waterloo
# Custom extent is basically the extent of the area that the overlay of hexagons will cover
# The third tells binify to ignore hexagons that have zero points
binify wloo_violent.shp wloo_violent_binify.shp -n 45 -E -92.431873 -92.260867 42.4097259 42.558403 --exclude-empty
# Convert binified shp into JSON file called "crime_data_review_violent"
ogr2ogr -f "GeoJSON" crime_data_review_violent.json wloo_violent_binify.shp
# Add this variable to JSON file
var crime_data_review_violent
# Here's the final files:
https://github.com/csessig86/crime_map2013/tree/master/shps/wloo_shps
# And our JSON file:
https://github.com/csessig86/crime_map2013/blob/master/JSON/crime_data_review_violent.json
# And the Javascript file that is used with Leaflet to run the map.
# Lines 113 through 291 are the ones related to pulling the data from the JSON files,
# coloring them, setting the legend and setting a mouseover for each hexagon
https://github.com/csessig86/crime_map2013/blob/master/js/script_map_review.js
# And the map online:
http://wcfcourier.com/app/crime_map2013/index_wloo.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment