0

I have created three buffers using geopandas in python and I have a few points scattered here and there, some of them are inside the buffers.But the problem is the following:

  1. When I obtain distance between the buffer centers and the points, for those points which are inside the buffer the distance shown here is 0 and for others some value >0. But the problem may arise if we have all these randomly scattered points inside the buffers, then the distance from the center of the buffer to all these points is 0. But in that case you cannot sort the values distance wise (in meters) and find the minimum one?

My code here is thus:

    import pandas as pd
    import geopandas
    import numpy as np
    us_states= geopandas.read_file("C:/Users/guptac/Downloads/cb_2017_us_state_500k/cb_2017_us_state_500k.shp")
    print(us_states.geometry[0])

    from shapely.geometry import Point,Polygon,LineString
    import fiona
    import shapely
    from shapely.geometry import shape

    df = pd.DataFrame(
    {'City': ['Fargo', 'Orange', 'Jersey City'],
     'State': ['North dakota', 'California', 'New Jersey'],
     'Latitude': [46.877186 , 33.787914 , 40.728157 ],
     'Longitude': [-96.789803, -117.853104, -74.077644]})
    df['Coordinates']  = list(zip(df.Longitude, df.Latitude))
    df['Coordinates'] = df['Coordinates'].apply(Point)
    gdf = geopandas.GeoDataFrame(df, geometry='Coordinates')
    gdf_points=gdf
    print(gdf.head())

    world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))
    #nybb=  geopandas.read_file(geopandas.datasets.get_path('nybb'))

    # We restrict to USA.
    ax = world[world.name == 'United States'].plot(
    color='white', edgecolor='black',figsize=(20,10))
    us_states.plot(ax=ax,figsize=(20,10))
    gdf.plot(ax=ax, color='red')
    ax.set(xlim=(-140,-50),ylim=(0,60))
    us_states.crs
    gdf.crs= {'+init' :'epsg:4326'} 
    print(gdf.crs['units'])
    us_name = world[world.name=='United States']
    gdf['Coordinates']=gdf.buffer(3)
    #gdf.head(5)
    gplot=geopandas.overlay(us_states,gdf,how='identity')
    gx=gplot.plot(edgecolor='k', alpha=0.5, cmap='tab10', ax=ax)
    gdf.plot(ax=ax)

    df1=pd.DataFrame(
            {'Points':['Point1','Point2','Point3','Point4','Point5','Point6'],
             'Latitude':[44.773491,30.287765,41.279906,45.234565,31.23345,38.778987],
             'Longitude':[-94.789323,-114.565309,-72.055234,-93.230988,-112.233456,-72.239989]})
    df1['Coordinates']  = list(zip(df1.Longitude, df1.Latitude))
    df1['Coordinates'] = df1['Coordinates'].apply(Point)
    gdf1 = geopandas.GeoDataFrame(df1, geometry='Coordinates')
    print(gdf1.head()) 

    gdf1.plot(ax=gx,color='red')
    gx.set(xlim=(-140,-50),ylim=(0,60))




    #a = Point(-93.230988, 45.234565)
    centroid_of_buffers=gdf['Coordinates'].centroid
    #dist_a=centroid_of_buffers.distance(a)
    #min_dist=centroid_of_buffers.distance(gdf1[gdf1.Coordinates=="POINT (-93.230988 45.234565)"])
    gdf_distance_cluster_Nevada=gdf1.distance(gdf['Coordinates'][0],)
    gdf_distance_cluster_California=gdf1.distance(gdf['Coordinates'][1])
    gdf_distance_cluster_New_Jersey=gdf1.distance(gdf['Coordinates'][2])

From here what I can guess is how do I obtain the euclidean distance between the buffer centers and the randomly scattered points ?

You can find the us_states dataset from https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html

1 Answer 1

0

I have found out why this error comes in:

The distances you calculate are not exactly the distance between the centroids of the buffers and the randomly scattered points. What you have to do is this :

gdf_distance_cluster_Nevada=gdf1['Coordinates'].distance(gdf['Coordinates'][0].centroid)
gdf_distance_cluster_California=gdf1['Coordinates'].distance(gdf['Coordinates'][1].centroid)
gdf_distance_cluster_New_Jersey=gdf1['Coordinates'].distance(gdf['Coordinates'][2].centroid)

This is I think what you want.

Your Answer

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

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