OSMNx output shp without correct coordinates

Viewed 22

I'm trying to process some data with osmnx library with some code adaptions from the example.

The problem is that the shapefile output is coming with the wrong coordinate limits even though setting the proper CRS.

What am I missing?

%%time


for x,y,z in zip(metro['X'], metro['Y'], metro['Nome']):
    print(f'Next station {z}')
    
    if not os.path.exists(z):
      os.makedirs(z)

    point = (y,x)
    print(point)
    G = create_graph(loc = point, dist=2000, transport_mode='walk', loc_type = 'points')
    
    print('Data collected.')
    
    # find the centermost node and then project the graph to UTM
    gdf_nodes = ox.graph_to_gdfs(G, edges=False) 
    x, y = gdf_nodes['geometry'].unary_union.centroid.xy
    center_node = ox.get_nearest_node(G, (y[0], x[0]))
    G2 = ox.project_graph(G)
    print('Os nódulos já foram coletados.')
    
    # add an edge attribute for time in minutes required to traverse each edge
    meters_per_minute = travel_speed * 1000 / 60 #km per hour to m per minute
    for u, v, k, data in G2.edges(data=True, keys=True):
        data['time'] = data['length'] / meters_per_minute
    print('O tempo e velocidade de viagem já foram definidos.')
    
    # get one color for each isochrone
    iso_colors = ox.plot.get_colors(n=len(trip_times), cmap='plasma', start=0, return_hex=True)
    print('A cor para as isócronas já foi definida')
    
    # color the nodes according to isochrone then plot the street network
    node_colors = {}
    for trip_time, color in zip(sorted(trip_times, reverse=True), iso_colors):
        subgraph = nx.ego_graph(G2, center_node, radius=trip_time, distance='time')
        for node in subgraph.nodes():
            node_colors[node] = color
    nc = [node_colors[node] if node in node_colors else 'none' for node in G2.nodes()]
    ns = [15 if node in node_colors else 0 for node in G2.nodes()]
    print('A correlação entre os nódulos e as isócronas já foi feita.')
    
    # make the isochrone polygons
    isochrone_polys = []
    for trip_time in sorted(trip_times, reverse=True):
        subgraph = nx.ego_graph(G2, center_node, radius=trip_time, distance='time')
        node_points = [Point((data['x'], data['y'])) for node, data in subgraph.nodes(data=True)]
        bounding_poly = gpd.GeoSeries(node_points).unary_union.convex_hull
        isochrone_polys.append(bounding_poly)
    print('Os polígonos das isócronas já foram feitos.')
    
    #Creating isochrones
    isochrone_polys = make_iso_polys(G2, edge_buff=25, node_buff=0, infill=True)
    fig, ax = ox.plot_graph(G2, show=False, close=False, edge_color='#999999', edge_alpha=0.2,
                            node_size=0, bgcolor='k')
     #exporting isochrones to shp
    for polygon, fc in zip(isochrone_polys, iso_colors):
        patch = PolygonPatch(polygon, fc=fc, ec='none', alpha=0.6, zorder=-1)
        ax.add_patch(patch)
        export_poly = gpd.GeoSeries(polygon)
        export_poly.set_crs('EPSG:4326', inplace = True)
        print(export_poly.crs)
        export_poly.to_file(f'{z}/polygon_{z}_{datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")}.shp')

    plt.title(f'{z} Station')
    print('Agora vou salvar a imagem')
    plt.savefig(f'{z}/estacao_{z}_{datetime.now().strftime("%Y-%m-%d-%H-%M-%m")}.jpeg')

The output in QGIS shows how far it's from the original place - which is in Rio de Janeiro, Brazil.

enter image description here

0 Answers
Related