emax
emax

Reputation: 7245

Python: how to create square buffers around points with a distance in meters

I have a geopandas dataframe gdf

gdf
    ID  longitude   latitude    geometry
0   80  103.619501  1.2810      POINT (103.619500987 1.281)
1   81  103.619501  1.2855      POINT (103.619500987 1.2855)

Following this suggestion I create a square buffer around it with a distance bd defined as:

bd = abs((gdf['latitude'][0]-gdf['latitude'][1])/2)

And finally I am able to get the following:

buffer = gdf.buffer(bd)
envelope = buffer.envelope

f, ax = plt.subplots(figsize=(7.5, 7.5))
envelope.plot(color='white', edgecolor='gray',ax=ax)
gdf.plot(ax=ax)

enter image description here

How can I set a distance bd that corresponds to 500 meters?

Upvotes: 7

Views: 7874

Answers (2)

martinfleis
martinfleis

Reputation: 7804

You need to reproject your data to CRS which uses meters as coordinates. As you are using longitude and latitude, your values are in degrees. All, including buffer distance. If you want to do square buffer, you do not need to use envelope, just set cap_style to 3 (see shapely docs).

gdf.crs = 'epsg:4326' # I am assuming here
gdf = gdf.to_crs(epsg=3395)

buffer = gdf.buffer(500, cap_style=3) # you might want to use 250, guessing from your image

For more details I recommend geopandas user guide.

Upvotes: 8

happy hacking
happy hacking

Reputation: 21

this is my function, which have high precision. data is a DataFrame,which contain a shapely's geometry type

def generate_buffer_meter(data, radiu, geometry='geometry', crs='epsg:4326'):
    data = gpd.GeoDataFrame(data, geometry=geometry, crs=crs)
    data = data.to_crs('+proj=aeqd +units=m  +x_0=0 +y_0=0')
    data[geometry] = data[geometry].buffer(radiu)
    data = data.to_crs(crs)
    return data

Upvotes: 2

Related Questions