2

我想将此 JSON 响应中的坐标顺序从 (lat,lon) 反转为 (lon,lat):

url='https://www.sciencebase.gov/catalogMaps/mapping/ows/5342c5fce4b0aa151574a8ed?\
service=wfs&version=1.1.0&request=GetFeature&typeNames=sb:Conservation_Zone_WGS84&outputFormat=application/json'
response = requests.get(url).json()   
print response

{u'crs': {u'properties': {u'code': u'4326'}, u'type': u'EPSG'},
 u'features': [{u'geometry': {u'coordinates': [[[[39.81487959537135,
        -74.09688169446223],
       [39.81488113835475, -74.09587338924456],
       [39.8143317590967, -74.09614209870023],
       [39.8137616151959, -74.09633047532941],
       [39.812950626580545, -74.09670529470912],
       [39.8120075697193, -74.09698124228382],
       [39.814255381955064, -74.0973277412355],
       [39.81487959537135, -74.09688169446223]]]],
    u'type': u'MultiPolygon'},
   u'geometry_name': u'the_geom',
   u'id': u'Conservation_Zone_WGS84.1',
   u'properties': {u'ID': 1,
    u'NAME': u'Sedge Island Marine Conservation Zone',
    u'OBJECTID': 1,
    u'SHAPE_AREA': 70259289.0821,
    u'SHAPE_LEN': 40592.8006466,
    u'WEB_LINK': u'http://www.state.nj.us/dep/fgw/sedge.htm'},
   u'type': u'Feature'}],
 u'type': u'FeatureCollection'}

我可以把它拆开,用蛮力把它粘在一起,但我想知道:在保持结构完整的同时改变顺序的好方法是什么?

4

4 回答 4

4

使用它的解决方案numpy应该适用于任何geojson。它将翻转所有“坐标”。

import json
import requests
import numpy as np


def flip_geojson_coordinates(geo):
    if isinstance(geo, dict):
        for k, v in geo.iteritems():
            if k == "coordinates":
                z = np.asarray(geo[k])
                f = z.flatten()
                geo[k] = np.dstack((f[1::2], f[::2])).reshape(z.shape).tolist()
            else:
                flip_geojson_coordinates(v)
    elif isinstance(geo, list):
        for k in geo:
            flip_geojson_coordinates(k)

url = "https://www.sciencebase.gov/catalogMaps/mapping/ows/5342c5fce4b0aa151574a8ed?\
service=wfs&version=1.1.0&request=GetFeature&typeNames=sb:Conservation_Zone_WGS84&outputFormat=application/json"
resp = requests.get(url)
gj = json.loads(resp.text)

print gj
flip_geojson_coordinates(gj)
print gj
于 2014-04-11T19:52:21.773 回答
2

使用列表理解并将结果重新分配给结构。

这里涉及到几个列表,所以需要几个循环:

for feature in response['features']:
    feature['geometry']['coordinates'] = [[
        [[long, lat] for lat, long in coords] for coords in poly]
        for poly in feature['geometry']['coordinates']]

这确实假设 的结构'coordinates'是稳定的;我看到也有一个'type'键,如果使用了其他类型,您可能必须改变更改结构u'MultiPolygon的方式。

这会将数据从:

>>> pprint.pprint(response)
{u'crs': {u'properties': {u'code': u'4326'}, u'type': u'EPSG'},
 u'features': [{u'geometry': {u'coordinates': [[[[39.81487959537135,
                                                  -74.09688169446223],
                                                 [39.81488113835475,
                                                  -74.09587338924456],
                                                 [39.8143317590967,
                                                  -74.09614209870023],
                                                 [39.8137616151959,
                                                  -74.09633047532941],
                                                 ....
                                                 [39.814255381955064,
                                                  -74.0973277412355],
                                                 [39.81487959537135,
                                                  -74.09688169446223]]]],
                              u'type': u'MultiPolygon'},
                u'geometry_name': u'the_geom',
                u'id': u'Conservation_Zone_WGS84.1',
                u'properties': {u'ID': 1,
                                u'NAME': u'Sedge Island Marine Conservation Zone',
                                u'OBJECTID': 1,
                                u'SHAPE_AREA': 70259289.0821,
                                u'SHAPE_LEN': 40592.8006466,
                                u'WEB_LINK': u'http://www.state.nj.us/dep/fgw/sedge.htm'},
                u'type': u'Feature'}],
 u'type': u'FeatureCollection'}

至:

>>> pprint.pprint(response)
{u'crs': {u'properties': {u'code': u'4326'}, u'type': u'EPSG'},
 u'features': [{u'geometry': {u'coordinates': [[[[-74.09688169446223,
                                                  39.81487959537135],
                                                 [-74.09587338924456,
                                                  39.81488113835475],
                                                 [-74.09614209870023,
                                                  39.8143317590967],
                                                 [-74.09633047532941,
                                                  39.8137616151959],
                                                 ....
                                                 [-74.0973277412355,
                                                  39.814255381955064],
                                                 [-74.09688169446223,
                                                  39.81487959537135]]]],
                              u'type': u'MultiPolygon'},
                u'geometry_name': u'the_geom',
                u'id': u'Conservation_Zone_WGS84.1',
                u'properties': {u'ID': 1,
                                u'NAME': u'Sedge Island Marine Conservation Zone',
                                u'OBJECTID': 1,
                                u'SHAPE_AREA': 70259289.0821,
                                u'SHAPE_LEN': 40592.8006466,
                                u'WEB_LINK': u'http://www.state.nj.us/dep/fgw/sedge.htm'},
                u'type': u'Feature'}],
 u'type': u'FeatureCollection'}
于 2014-04-11T17:41:50.363 回答
2

好吧......深入到你的坐标列表需要这个:

d['features'][0]['geometry']['coordinates'][0][0]

所以要扭转那些你必须这样做:

d['features'][0]['geometry']['coordinates'][0][0] = [i[::-1] for i in d['features'][0]['geometry']['coordinates'][0][0]]

或者,更清洁的 IMO:

for l in d['features'][0]['geometry']['coordinates'][0][0]:
    l.reverse()
于 2014-04-11T17:42:42.580 回答
2

您的“pythonic”代码不可读且不起作用。把事情简单化:

def swapCoords(x):
    out = []
    for iter in x:
        if isinstance(iter, list):
            out.append(swapCoords(iter))
        else:
            return [x[1], x[0]]
    return out



for feature in response['features']:
    feature['geometry']['coordinates'] = swapCoords(feature['geometry']['coordinates'])
于 2015-12-16T14:22:06.903 回答