summaryrefslogtreecommitdiff
path: root/localosmfeature2geojson
blob: 1389a8225af51e8014b180250293ff603d520456 (plain)
  1. #!/bin/sh
  2. #
  3. # Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
  4. # Description: resolve GeoJSON from OSM way or relation.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # Depends: gdal-bin perl-modules
  20. set -eu
  21. # type must be either polygons or lines
  22. type="$1"; shift
  23. features=$*
  24. onlineosmxml2geojson() {
  25. set -e
  26. echo '['
  27. first=yes
  28. for feature in "$@"; do
  29. url="http://www.openstreetmap.org/api/0.6/$feature/full"
  30. [ -n "$first" ] || echo ','
  31. case "$type" in
  32. polygons)
  33. ogr2ogr --config OSM_USE_CUSTOM_INDEXING NO -nlt PROMOTE_TO_MULTI -f GeoJSON /vsistdout/ /vsicurl_streaming/"$url" multipolygons
  34. ;;
  35. lines)
  36. ogr2ogr --config OSM_USE_CUSTOM_INDEXING NO -nlt PROMOTE_TO_MULTI -f GeoJSON /vsistdout/ /vsicurl_streaming/"$url" multilinestrings
  37. ;;
  38. esac
  39. first=
  40. done
  41. echo ']'
  42. }
  43. onlineosmxml2geojson $features \
  44. | json_pp -json_opt pretty,canonical \
  45. | perl -0 -pe 's/ /\t/g; s/},\s*{/}, {/g; s/\[\s*([\d.,]+)\s*([\d.]+)\s*\]/[$1 $2]/g'