2

This question already has an answer here:

I am trying to create square buffers around given points, I am able to create circular buffers but how do I create square ones?

from shapely.ops import transform
from shapely.geometry import Point
    local_azimuthal_projection = "+proj=aeqd +R=6371000 +units=m +lat_0={} +lon_0={}".format(lat, lon)
                    wgs84_to_aeqd = partial(
                        pyproj.transform,
                        pyproj.Proj('+proj=longlat +datum=WGS84 +no_defs'),
                        pyproj.Proj(local_azimuthal_projection),
                    )
                    aeqd_to_wgs84 = partial(
                        pyproj.transform,
                        pyproj.Proj(local_azimuthal_projection),
                        pyproj.Proj('+proj=longlat +datum=WGS84 +no_defs'),
                    )

                    point_transformed = transform(wgs84_to_aeqd, Point(float(lon), float(lat)))
                    buffer = point_transformed.buffer(0.5*1000)
                    buffered_geom = transform(aeqd_to_wgs84, buffer).exterior.coords[:]

marked as duplicate by gene python Mar 10 at 8:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

One common approach is to first apply a circular buffer and then apply an envelope (i.e. smallest rectangular polygon that contains each object).

Here is how you can create square buffers using geopandas GeoSeries.envelope (source). Note that geometric operations in geopandas are performed by shapely.

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt

# Generate some sample data 
p1 = Point((1,2))
p2 = Point((5,6))
df = pd.DataFrame({'a': [11,22]})
gdf = gpd.GeoDataFrame(df, geometry = [p1,p2])
gdf.plot()

enter image description here

# Buffer the points by 2 units
buffer = gdf.buffer(2)
buffer.plot()

enter image description here

# Apply an envelope around circular buffers
envelope = buffer.envelope  
envelope.plot()

enter image description here

  • What is a here in creating df? – Atihska Mar 10 at 19:52
  • Just populating a field in the data frame when creating sample data. You can disregard it if you wish. – Aaron Mar 10 at 19:54
  • I meant what is the difference between p1, p2 and a? – Atihska Mar 10 at 19:59
  • p1 and p2 are points in a cartesian coordinate system--essentially just made up sample data in which I applied the describe geometric operations. a is some attribute field that you can disregard if it adds confusion. – Aaron Mar 11 at 2:47

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