[Tutor] Help with two .shp file and python coding

ThreeBlindQuarks threesomequarks at proton.me
Tue Mar 28 10:43:44 EDT 2023


Tariq,

Your entire program is written in a module called geopandas so to even understand it, we would need to find out what exactly various functions in that module you call are documented to do.

A little more explanation of what you are doing might be helpful.

Near as I can tell, you are reading in two files to make two structures in a pandas format. You join them based on some common columns in whatever manner gpd.sjoin_nearest() does so that each row now contains enough info for the next step.

You create a function that accepts one such row and extracts the coordinates of two parts and calculates a distance that I am guessing is a Euclidean distance as the crow flies, not as you drive on roads. 

You use that function on every row to create a new column.

Then you save the results in a file.

Does that do something? Who knows. The devil is in the details. The mysterious black box that does the initial join would have to uniquely match up counties and highways in a way that generates exactly the rows needed and exclude any that don't. We (meaning ME) have no assurance of what happens in there. Could you end up comparing all counties with their distance from somewhere on highway 1?

- Q


Sent with Proton Mail secure email.

------- Original Message -------
On Monday, March 27th, 2023 at 10:58 PM, Tariq Khasiri <tariqkhasiri at gmail.com> wrote:


> Hello everyone,
> 
> For a particular project, I need to collect the distance data from each US
> county to the nearest intersection of interstate highway.
> 
> These are the publicly provided dataset where anyone has access to the
> county shp files of usa and roads shp files.
> 
> this one for county polygons
> https://public.opendatasoft.com/explore/dataset/us-county-boundaries/information/
> 
> this one for primary roads
> 
> https://catalog.data.gov/dataset/tiger-line-shapefile-2016-nation-u-s-primary-roads-national-shapefile/resource/d983eeef-21d9-4367-9d42-27a131ee72b8
> 
> `import geopandas as gpd from shapely.geometry import Point # Load shapefiles counties = gpd.read_file('path/to/counties.shp') interstates = gpd.read_file('path/to/interstates.shp') # Perform spatial join to find nearest interstate for each county joined = gpd.sjoin_nearest(counties, interstates) # Calculate distance between each county and its nearest interstate def calculate_distance(row): county_point = row.geometry.x, row.geometry.y interstate_point = row.geometry_nearest.x, row.geometry_nearest.y return Point(county_point).distance(Point(interstate_point)) joined['distance'] = joined.apply(calculate_distance, axis=1) # Save results to file joined.to_file('path/to/output.shp')`
> 
> do you think I can execute my goal successfully with the code snippet above
> ??
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list