Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Extract the US zip-code shapes and convert to a single US-wide
  4. # GeoJSON file
  5. echo -n "Extracting US-wide GeoJSON file (takes ~10 mins)..."
  6. mkdir tl_2014_us_zcta510
  7. unzip -qq tl_2014_us_zcta510.zip -d tl_2014_us_zcta510
  8. ogr2ogr -f GeoJSON -t_srs crs:84 tl_2014_us_zcta510.geojson tl_2014_us_zcta510/tl_2014_us_zcta510.shp
  9. rm -r tl_2014_us_zcta510
  10. echo "done."
  11.  
  12. # Join the {zip->city} mapping to create per-city files with the
  13. # GeoJSON sub-documents for each zip-code in the city.
  14. echo -n "Creating per-city partial GeoJSON files (takes ~15 mins)..."
  15. mkdir per_city_partials
  16. join -1 2 -2 1 <(sort -k 2,2 us_city_zips.tsv) <(cat tl_2014_us_zcta510.geojson | gawk '{ match( $0 , /ZCTA5CE10\": \"([0-9]+)\"/, arr ); if ( arr[1] != "" ) print arr[1], $0 }' | sort) | sed 's/,$//g' | gawk '{ city="per_city_partials/" $2; $1=$2=""; print $0 >> city; close(city) }'
  17. echo "done"
  18.  
  19. # For each city, combine the zip-code GeoJSON sub-documents into a
  20. # single city-level GeoJSON document. Also annotate the resulting
  21. # GeoJSON document with properties for the city ID and city name.
  22. echo "Combining partial GeoJSON files into complete per-city GeoJSON files with annotations (takes ~2 hours)..."
  23. mkdir per_city_geojson
  24. for i in per_city_partials/*
  25. do
  26. id="${i#*/}"
  27. state="${id%%_*}"
  28. city="${id#*_}"
  29. city="${city//_/ }"
  30. cat <( echo '{ "type":"FeatureCollection", "features":[' ; gawk '{ if ( NR > 1 ) print "," ; print $0 }' ${i} ; echo "]}" ) | mapshaper - -dissolve2 -simplify 10% -each "\$.properties = { id: \"${id}\", state: \"${state}\", city: \"${city}\" }" -o per_city_geojson/${id}.json
  31. done
  32. echo "Done generating complete per-city GeoJSON files."
  33.  
  34. # Catenate the per-city GeoJSON files into a single US-wide GeoJSON
  35. # file.
  36. echo -n "Combine per-city GeoJSON files into single US-wide GeoJSON file..."
  37. echo '{"type":"FeatureCollection","features":[' > us_cities.geojson
  38. for i in per_city_geojson/*
  39. do
  40. cat ${i}
  41. echo
  42. done | sed 's/^[{]"type"[:]"FeatureCollection"[,]"features"[:][[]//' | sed 's/\]\}$//' | awk '{ if ( NR > 1 ) printf( "%s", ", ") ; print $0 }' >> us_cities.geojson
  43. echo ']}' >> us_cities.geojson
  44. echo "done."
  45.  
  46. # Last, but not least, convert GeoJSON into a Shapefile .zip
  47. echo "Convert US-wide GeoJSON file to shapefile .zip..."
  48. mkdir shapefile
  49. cd shapefile
  50. ogr2ogr -F "ESRI Shapefile" us_cities.shp ../us_cities.geojson OGRGeoJSON
  51. zip -qq ../us_cities.zip us_cities.dbf us_cities.prj us_cities.shp us_cities.shx
  52. cd ..
  53. rm -rf shapefile
  54. echo "done."
  55.  
  56. echo ; echo "All done!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement