【问题标题】:Convert google directions into a shapefile line将 google 方向转换为 shapefile 行
【发布时间】:2017-02-03 21:22:55
【问题描述】:

是否可以获取从 Google 路线 API 返回的 json 并将该信息转换为与路线折线相同的 shapefile 线?

我想在 QGIS 中制作的地图上绘制一次旅行。

【问题讨论】:

    标签: python json python-2.7 google-maps gis


    【解决方案1】:

    我终于想出了如何使用 FionaShapely 以及我在 GitHub 上找到的 function 并进行修改以适应:

    import json
    import fiona
    import pandas as pd
    from shapely.geometry import LineString, mapping
    
    def decode_polyline(polyline_str):
        '''Pass a Google Maps encoded polyline string; returns list of lat/lon pairs'''
        index, lat, lng = 0, 0, 0
        coordinates = []
        changes = {'latitude': 0, 'longitude': 0}
    
        # Coordinates have variable length when encoded, so just keep
        # track of whether we've hit the end of the string. In each
        # while loop iteration, a single coordinate is decoded.
        while index < len(polyline_str):
            # Gather lat/lon changes, store them in a dictionary to apply them later
            for unit in ['latitude', 'longitude']: 
                shift, result = 0, 0
    
                while True:
                    byte = ord(polyline_str[index]) - 63
                    index+=1
                    result |= (byte & 0x1f) << shift
                    shift += 5
                    if not byte >= 0x20:
                        break
    
                if (result & 1):
                    changes[unit] = ~(result >> 1)
                else:
                    changes[unit] = (result >> 1)
    
            lat += changes['latitude']
            lng += changes['longitude']
    
            coordinates.append((lng / 100000.0, lat / 100000.0))
    
        return coordinates
    
    def get_linestring(trip_name):
        with open(trip_name + '.json', 'r') as data_file:    
            data = json.load(data_file, encoding='ISO-8859-1')
    
        the_points = []
        for step in data['routes'][0]['legs'][0]['steps']:
            the_points += decode_polyline(step['polyline']['points'])
    
        return LineString(the_points)
    
    
    if __name__ == '__main__':
        trip_names = ['trip1', 'trip2', 'trip3']
    
        driver = 'ESRI Shapefile'
        crs = {'no_defs': True, 
                'ellps': 'WGS84', 
                'datum': 'WGS84', 
                'proj': 'longlat'}
        schema = {'geometry': 'LineString', 'properties': {'route': 'str'}}
        with fiona.open('all_trips.shp', 'w', driver=driver, crs=crs, schema=schema) as layer:
            for trip_name in trip_names:
                layer.write({'geometry': mapping(get_linestring(trip_name)),
                                'properties': {'route': trip_name}
                                })
    

    代码假定 json 文件包含来自 Google 地图 API 的 json 响应,并且与代码位于同一文件夹中。对给定行程的折线进行解码,然后使用 Shapely 转换为 LineString,然后使用 Fiona 将每个 LineString 保存到 shapefile。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2020-09-06
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多