从GeoJSON加载的Python 3.X Shapely Polygon

时间:2018-12-05 22:08:28

标签: python openstreetmap shapely

我想使用 Shapely 包定义一个多边形,其点来自OpenStreetMap。让我们来看一个例子。

我从OpenStreetMap下载了以下export.geojson,其中包含纽约市地狱厨房区的边界点。

{
  "type": "FeatureCollection",
  "generator": "overpass-ide",
  "copyright": "The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.",
  "timestamp": "2018-12-05T21:48:03Z",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "@id": "relation/8398096",
        "admin_level": "10",
        "alt_name": "Midtown West",
        "boundary": "administrative",
        "name": "Hell's Kitchen",
        "place": "neighbourhood",
        "type": "boundary",
        "wikidata": "Q840133"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -73.996317,
              40.7533999
            ],
            [
              -73.9926276,
              40.7584281
            ],
            [
              -73.9897925,
              40.7572376
            ],
            [
              -73.9865711,
              40.7616564
            ],
            [
              -73.9828759,
              40.7667118
            ],
            [
              -73.9857128,
              40.7679195
            ],
            [
              -73.9847606,
              40.7692277
            ],
            [
              -73.993569,
              40.7729369
            ],
            [
              -73.9939928,
              40.7733086
            ],
            [
              -73.9941524,
              40.7733787
            ],
            [
              -73.9960903,
              40.7742003
            ],
            [
              -73.9963692,
              40.7738002
            ],
            [
              -73.9940733,
              40.7727927
            ],
            [
              -73.9942235,
              40.7725956
            ],
            [
              -73.99616,
              40.7733838
            ],
            [
              -73.9964068,
              40.7730913
            ],
            [
              -73.9946607,
              40.7723438
            ],
            [
              -73.9948565,
              40.7720959
            ],
            [
              -73.9969674,
              40.7729836
            ],
            [
              -73.9973322,
              40.7725205
            ],
            [
              -73.9952078,
              40.7716166
            ],
            [
              -73.995409,
              40.7713606
            ],
            [
              -73.995975,
              40.7716003
            ],
            [
              -73.9961976,
              40.7712915
            ],
            [
              -73.9959508,
              40.771188
            ],
            [
              -73.9959937,
              40.771129
            ],
            [
              -73.9956531,
              40.7709889
            ],
            [
              -73.9958328,
              40.7707451
            ],
            [
              -73.9959213,
              40.7707878
            ],
            [
              -73.9962056,
              40.7704465
            ],
            [
              -73.996093,
              40.7703896
            ],
            [
              -73.9963827,
              40.7699692
            ],
            [
              -73.9987564,
              40.7709279
            ],
            [
              -73.9991614,
              40.7703917
            ],
            [
              -73.9968413,
              40.769376
            ],
            [
              -73.9971685,
              40.7689454
            ],
            [
              -73.9995879,
              40.7699773
            ],
            [
              -74.0000251,
              40.7694248
            ],
            [
              -73.9967179,
              40.7680576
            ],
            [
              -73.9972463,
              40.7673263
            ],
            [
              -74.0005133,
              40.7687077
            ],
            [
              -74.0009397,
              40.7681714
            ],
            [
              -73.9976245,
              40.7667697
            ],
            [
              -73.998,
              40.76627
            ],
            [
              -73.9982522,
              40.766268
            ],
            [
              -74.0013501,
              40.7675681
            ],
            [
              -74.0019402,
              40.7668043
            ],
            [
              -73.9987242,
              40.7654147
            ],
            [
              -73.9989388,
              40.7652502
            ],
            [
              -73.9988717,
              40.7649861
            ],
            [
              -73.9991775,
              40.7649556
            ],
            [
              -74.0021253,
              40.7661115
            ],
            [
              -74.0026027,
              40.765498
            ],
            [
              -74.0024284,
              40.7654066
            ],
            [
              -74.0025812,
              40.7650897
            ],
            [
              -73.9995718,
              40.7638444
            ],
            [
              -74.000001,
              40.7636087
            ],
            [
              -74.0031847,
              40.7649516
            ],
            [
              -74.0034664,
              40.764594
            ],
            [
              -74.003225,
              40.7644721
            ],
            [
              -74.0033671,
              40.764273
            ],
            [
              -74.0012401,
              40.7633487
            ],
            [
              -74.0014815,
              40.7630358
            ],
            [
              -74.0036756,
              40.7639541
            ],
            [
              -74.0040135,
              40.7635214
            ],
            [
              -74.0018356,
              40.7625787
            ],
            [
              -74.0023103,
              40.7619571
            ],
            [
              -74.0045071,
              40.7628672
            ],
            [
              -74.004837,
              40.7624447
            ],
            [
              -74.0025732,
              40.7614573
            ],
            [
              -74.0028682,
              40.761051
            ],
            [
              -73.9964148,
              40.7583347
            ],
            [
              -73.9979787,
              40.7561892
            ],
            [
              -73.9991507,
              40.754582
            ],
            [
              -73.996317,
              40.7533999
            ]
          ]
        ]
      },
      "id": "relation/8398096"
    },
    {
      "type": "Feature",
      "properties": {
        "@id": "node/158852213",
        "@relations": [
          {
            "role": "label",
            "rel": 8398096,
            "reltags": {
              "admin_level": "10",
              "alt_name": "Midtown West",
              "boundary": "administrative",
              "name": "Hell's Kitchen",
              "place": "neighbourhood",
              "type": "boundary",
              "wikidata": "Q840133"
            }
          }
        ]
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -73.9923918,
          40.7644228
        ]
      },
      "id": "node/158852213"
    }
  ]
}

基于documentation Integration 部分和this book的第89页,我得到了以下Python代码段,用于定义形状为Polygon的{地狱的厨房:

from shapely.geometry import mapping, shape
I=shape(json.loads(open('/Users/MyUsername/Downloads/export.geojson').read()))
I.is_valid

但是出现以下错误:

  

回溯(最近一次通话最后一次):文件“”,第1行,在          I.is_valid()NameError:名称“ I”未定义

     
    
      
        

I.is_valid追溯(最近一次通话最后一次):文件“”,第1行,在             I.is_valid NameError:名称“ I”未定义         有人对如何解决有想法吗?

      
    
  

我认为这不是重复的问题,因为this quesion与GeoJSON格式的外部文件无关。

0 个答案:

没有答案