pythongisshapefile

Creating a shape file from a bounding box coordinates list


There is already few existing questions about this topic, but I unfortunately did not find something that could fix my problem.

I have a point Lat, Long coordinate i.e. Lat= 10 and Long = 10. I want to create a shape file of a 0.5 degree bounding box around this point, so the bounding box should be as follow:

  1. minimum Long= 9.75
  2. minimum Lat = 9.75
  3. maximum Long = 10.25
  4. maximum Lat = 10.25

Does anyone knows how to do that in Python?


Solution

  • Here's one way to do it using shapely, geopandas and pandas:

    import geopandas as gpd
    import pandas as pd
    from shapely.geometry import Polygon
    
    
    def bbox(lat,lng, margin):                                                                                                                  
        return Polygon([[lng-margin, lat-margin],[lng-margin, lat+margin],
        [lng+margin,lat+margin],[lng+margin,lat-margin]])
    
    gpd.GeoDataFrame(pd.DataFrame(['p1'], columns = ['geom']),
         crs = {'init':'epsg:4326'},
         geometry = [bbox(10,10, 0.25)]).to_file('poly.shp')