도메인 domains/위치 기반 Location-based

Q. OSM 파일을 GeoJSON으로 변환하면 어떻게 바뀔까?

Tap to restart 2023. 10. 8. 10:00
반응형

A. GeoJSON 데이터가 Feature로 바뀐다.

 

osm을 GeoJSON으로 바꿔주는 파이썬 팩키지 osm2geojson을 활용해서 바꿔보자.

코드는 아래와 같다. 예제 코드를 그대로 활용하고 결과인 json을 파일로 저장한 코드다.

import codecs
import json

import osm2geojson

with codecs.open('sample.osm', 'r', encoding='utf-8') as data:
    xml = data.read()

geojson = osm2geojson.xml2geojson(xml, filter_used_refs=False, log_level='INFO')

with open("result.json", "w") as f:
    json.dump(geojson, f)

 

learnosm.org에 나온 예제 OSM 파일을 변환해봤다.

sample.osm
0.02MB

 

변환 결과로 result.json이 나왔다.

result.json
0.02MB

json 전체를 다 보기에는 양이 많으니 사례별로 살펴보자.

 

osm → FeatureCollection

osm 파일은 어떻게 변환되었을까?

<?xml version='1.0' encoding='UTF-8'?>
<osm version='0.6' upload='never' generator='JOSM'>
...
</osm>

FeatureCollection으로 변환된다.

{
  "type": "FeatureCollection",
  "features": [
  ...
  ]
}

 

node 노드 → Feature - Point

node는 어떻게 변환되었을까?

  <node id='-792' action='modify' visible='true' lat='0.04927079896609012' lon='0.1091822051177659' /

Feature가 되고, 기하학 유형은 Point가 된다.

    {
      "type": "Feature",
      "properties": {
        "type": "node",
        "id": -792
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          0.1091822051177659,
          0.04927079896609012
        ]
      }
    },

 

만약 node에 태그 정보가 있는 경우라면 어떻게 될까?

  <node id='-603' action='modify' visible='true' lat='0.05992435104555641' lon='0.05138006817705822'>
    <tag k='name' v='Chapman&apos;s Shoes' />
    <tag k='shop' v='shoes' />
  </node>

아래처럼 properties에 해당 tag 정보가 기록된다.

    {
      "type": "Feature",
      "properties": {
        "type": "node",
        "id": -603,
        "tags": {
          "name": "Chapman's Shoes",
          "shop": "shoes"
        }
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          0.05138006817705822,
          0.05992435104555641
        ]
      }
    },

 

way 길 → Feature - LineString

way는 어떻게 변환되었을까?

  <way id='-715' action='modify' visible='true'>
    <nd ref='-511' />
    <nd ref='-513' />
    <tag k='highway' v='residential' />
    <tag k='name' v='Maron Lane' />
  </way>

LineString으로 변환되었다.

JSOM의 유형 way는 속성properties으로 기록된 것을 볼 수 있다.

    {
      "type": "Feature",
      "properties": {
        "type": "way",
        "id": -715,
        "tags": {
          "highway": "residential",
          "name": "Maron Lane"
        },
        "nodes": [
          -511,
          -513
        ]
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            0.049234163196502985,
            0.05951583980799796
          ],
          [
            0.059792952869964715,
            0.06813703179225494
          ]
        ]
      }
    },

 

닫혀 있는 way → Feature - Poligon

아래 처럼 닫혀 있는 way는 어떻게 변환되었을까?

  <way id='-727' action='modify' visible='true'>
    <nd ref='-555' />
    <nd ref='-557' />
    <nd ref='-559' />
    <nd ref='-561' />
    <nd ref='-555' />
    <tag k='building' v='yes' />
    <tag k='building:levels' v='1' />
    <tag k='building:use' v='residential' />
  </way>

Feature 유형을 사용해서 변경되었고, 기하학 유형은 Polygon이 되었다. way이지만 처음과 끝의 노드가 -555로 같아 닫혀 있는 경우라 Polygon이 된 것이다.

    {
      "type": "Feature",
      "properties": {
        "type": "way",
        "id": -727,
        "tags": {
          "building": "yes",
          "building:levels": "1",
          "building:use": "residential"
        },
        "nodes": [
          -555,
          -557,
          -559,
          -561,
          -555
        ]
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              0.050714075401954134,
              0.0598448595190833
            ],
            [
              0.051476392233545144,
              0.0589755513188258
            ],
            [
              0.0524184058357087,
              0.05980162434158052
            ],
            [
              0.05165608900411768,
              0.060670932528751706
            ],
            [
              0.050714075401954134,
              0.0598448595190833
            ]
          ]
        ]
      }
    },

 

JOSM에서 GeoJSON으로 변환시 위 예제 파일에서는 유실되는 정보가 없는 것을 볼 수 있다.

 

 

관련 글

위 내용이 잘 이해가 안 된다면 아래 두 글을 참고하자.

 

Q. GeoJSON이란? GeoJSON을 이루는 객체 종류는?

JOSM 주요 용어Terminology 살펴보기

반응형