2

I am still new to GeoPandas.

The issue I'm running into this time is although I have projected my gdf, the buffers created still are much too large. I've run this with a number of different projections (https://spatialreference.org/ref/?search=california) for Zone 2 and I still wind up with the following output:

enter image description here

Here is my code:

latlon = (points['y'].iloc[0],points['x'].iloc[0])
cnode = ox.get_nearest_node(sacroads, latlon)
subgraph = nx.ego_graph(sacroads, cnode, radius=trip_time, distance="time")
node_points = [Point((data["x"], data["y"])) for node, data in subgraph.nodes(data=True)]
nodes_gdf = gp.GeoDataFrame({"id": list(subgraph.nodes)}, geometry=node_points)
nodes_gdf = nodes_gdf.set_index("id")
edge_lines = []
for n_fr, n_to in subgraph.edges():
    f = nodes_gdf.loc[n_fr].geometry
    t = nodes_gdf.loc[n_to].geometry
    edge_lookup = sacroads.get_edge_data(n_fr, n_to)[0].get("geometry", LineString([f, t]))
    edge_lines.append(edge_lookup)
elines = gp.GeoDataFrame(gp.GeoSeries(edge_lines))
elines.columns = ['geometry']
elines = elines.set_geometry('geometry')
elines = elines.set_crs(epsg=3491)
elines['geometry'] = elines.geometry.buffer(10)
1

2 Answers 2

5

As I mentioned in the comments, your post here is bit difficult to work through; and to me, it's doing what I'm expecting: namely, buffering a point (-120, 40) by 10 results in the appropriately sized Polygon form (-130 to -110, 30 to 50).

However, I think I understand what your confusion is. You're setting the coordinate system here:

elines = elines.set_crs(epsg=3491)

This does nothing to the coordinates! A (longitude, latitude) pair of (-120, 40) simply becomes a xy pair of (-120, 40). You're looking for reprojection instead... which looks almost the same, to be fair:

elines = elines.to_crs(epsg=3491)

Here's a minimum working example on my part:

import geopandas as gpd

gdf = gpd.GeoDataFrame(geometry=gpd.points_from_xy([-110.0], [40.0]))
gdf = gdf.set_crs(epsg=4326)  # Set to WGS84; lat-lon in degrees
reprojected = gdf.to_crs(epsg=3491)  # Reproject; meters
print(reprojected)

This actually results in a reprojected geometry, with different coordinates:

                         geometry
0  POINT (3021794.294 826586.522)
1
  • Thanks! You reminded me that since I was creating a completely new dataframe I hadn't set the original CRS before projecting it. a quick line of code to add that fixed the issue :) Cheers!
    – deally94
    Commented May 3, 2021 at 0:54
2

In the next-to-last line of your code you use "set_crs" which tells the dataset it is in some crs; it doesn't actually transform the data. To do that, you need to use "to_crs".

I have no idea if the rest of your code is right as you don't provide the context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.