Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • calculating the distance from the intersection of interstate highway to each us county using .shp file

    Hello everyone,

    For a particular project, I need to collect the distance data from each US county to the nearest intersection of interstate highway ?

    Is there any way I can find the data where I can show the distance from each county to the nearest intersection of interstate highway by using stata??

    I know how to approach the problem using python like the following

    Code:
    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')
    If I update it to

    Code:
    joined = gpd.sjoin_nearest(interstates, counties, distance_cal='distance')
    it will create a column named distance with the distance between the nearest features from the 2 geodataframes. When an interstate intersects the county, the distance will be 0.0.

    Can anyone help me with this to converting it to stata ??
    Last edited by Tariq Abdullah; 27 Mar 2023, 08:22.

  • #2
    If it helps these are the following datasets , I'm planning to use. Both of them are public.

    This one for county polygons

    https://public.opendatasoft.com/expl...s/information/

    This one for primary roads

    https://catalog.data.gov/dataset/tig...2-27a131ee72b8

    Comment

    Working...
    X