summaryrefslogtreecommitdiff
path: root/localosmfeature2geojson
blob: 17be9a2a5c2a0bcccd2b3cd97c0fd9633b02c149 (plain)
  1. #!/bin/sh
  2. #
  3. # Copyright © 2014, 2017 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 jq
  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. | jq --tab --sort-keys . \
  55. | perl -0 -pe 's/},\s*{/}, {/g; s/\[\s*([\d.,]+)\s*([\d.]+)\s*\]/[$1 $2]/g'