User:Huybee3/Snippets View history

KML generation for Cities Overlay script

Instructions to generate KMLs for the Cities Overlay script using data from the 2019 Census ACS.

Tools Required

  • ogr2ogr: Converts between file formats for geometric data (Homebrew)
  • Mapshaper: Simplify geometric data (npm)
  • GNU sed: Much better than the version of sed provided by default on MacOS (Homebrew)

Cities and CDPs from Multiple States

The following set of commands will generate KMLs with Incorporated Places and CDPs for the entire PLN region.

wFIPs="19 20 27 29 31 38 46"
wStates="IA KS MN MO NE ND SD"
wLayers="28 30"

curl -o "TIGER_#1.json" "https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/tigerWMS_ACS2019/MapServer/{${wLayers// /,}}/query?" \
--data "where=STATE IN (${wFIPs// /%2C})" \
--data "outFields=STATE,BASENAME" \
--data "Geometry=true" \
--data "f=geojson"

eval mapshaper -i TIGER_{${wLayers// /,}}.json combine-files -merge-layers -simplify 40% -o - format=topojson quantization=1e6 -clean | mapshaper -i - name='' -simplify dp 80% -split STATE -o format=geojson precision=0.00001

wIndex=0; for wFIP in $(echo $wFIPs); do

let "wIndex++"; wState="$(cut -d ' ' -f $wIndex <<<"$wStates")"

ogr2ogr -f 'KML' "/vsistdout/" ${wFIP}.json -dsco NameField='BASENAME' |

grep -vE "^\s*<(Style>|ExtendedData>|Document |Folder>|Simple(Data|Field) |\/?Schema(Data)?)" |

gsed -E 's:(<kml .*):\1\n\t<Document>\n\t<Name>\'${State}'_Cities<\/Name>\n\t<visibility>1<\/visibility>\n<Folder id="\'${State}'_Cities"><Name>\'${State}'_Cities<\/Name>:' > ${wState}_Cities.kml

done

Cities Only for a Single State

The following set of commands will generate a KML with Incorporated Places only for the state of MO.

wFIP=29; wState="MO"; wLayer=28

curl "https://tigerweb.geo.census.gov/arcgis/rest/services/TIGERweb/tigerWMS_ACS2019/MapServer/${wLayer}/query?" \
--data "where=STATE IN (${wFIP})" \
--data "outFields=BASENAME" \
--data "Geometry=true" \
--data "f=geojson" |

mapshaper -i - -simplify 40% -o - format=topojson quantization=5e5 -clean | \
mapshaper -i - -simplify dp 80% name='' -o - format=geojson precision=0.00001 |

ogr2ogr -f 'KML' /vsistdout/ /vsistdin/ -dsco NameField='BASENAME' |

grep -vE "^\s*<(Style>|ExtendedData>|Document |Folder>|Simple(Data|Field) |\/?Schema(Data)?)" |

gsed -E 's:(<kml .*):\1\n\t<Document>\n\t<Name>\'${State}'_Cities<\/Name>\n\t<visibility>1<\/visibility>\n<Folder id="\'${State}'_Cities"><Name>\'${State}'_Cities<\/Name>:' > ${wState}_Cities.kml