summaryrefslogtreecommitdiff
path: root/localosmfeature2geojson
blob: dfd1a26bf3bc4bc3334ef254e9efb36f26469aa5 (plain)
  1. #!/bin/sh
  2. #
  3. # Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
  4. # Description: resolve GeoJSON from OSM node, way and relation objects.
  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, lines or points
  22. type="$1"; shift
  23. features=$*
  24. onlineosmxml2geojson() {
  25. set -e
  26. echo '['
  27. first=yes
  28. for feature in "$@"; do
  29. case "$feature" in
  30. node/*)
  31. url="http://www.openstreetmap.org/api/0.6/$feature"
  32. ;;
  33. *)
  34. url="http://www.openstreetmap.org/api/0.6/$feature/full"
  35. ;;
  36. esac
  37. [ -n "$first" ] || echo ','
  38. case "$type" in
  39. polygons)
  40. ogr2ogr --config OSM_USE_CUSTOM_INDEXING NO -nlt PROMOTE_TO_MULTI -f GeoJSON /vsistdout/ /vsicurl_streaming/"$url" multipolygons
  41. ;;
  42. lines)
  43. ogr2ogr --config OSM_USE_CUSTOM_INDEXING NO -nlt PROMOTE_TO_MULTI -f GeoJSON /vsistdout/ /vsicurl_streaming/"$url" multilinestrings
  44. ;;
  45. points)
  46. ogr2ogr --config OSM_USE_CUSTOM_INDEXING NO -f GeoJSON /vsistdout/ /vsicurl_streaming/"$url" points
  47. ;;
  48. esac
  49. first=
  50. done
  51. echo ']'
  52. }
  53. onlineosmxml2geojson $features \
  54. | json_pp -json_opt pretty,canonical \
  55. | perl -0 -pe 's/ /\t/g; s/},\s*{/}, {/g; s/\[\s*([\d.,]+)\s*([\d.]+)\s*\]/[$1 $2]/g'