简体   繁体   English

从边界框坐标列表创建形状文件

[英]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 ie 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:我有一个点Lat,Long坐标,即Lat= 10和Long = 10。我想在这个点周围创建一个0.5度边界框的形状文件,所以边界框应该如下:

  1. minimum Long= 9.75最小多头= 9.75
  2. minimum Lat = 9.75最小纬度 = 9.75
  3. maximum Long = 10.25最大多头 = 10.25
  4. maximum Lat = 10.25最大纬度 = 10.25

Does anyone knows how to do that in Python?有谁知道如何在 Python 中做到这一点?

Here's one way to do it using shapely, geopandas and pandas:这是使用 shapely、geopandas 和 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')

I want to enchance Bruno Carballo's code.我想增强 Bruno Carballo 的代码。 I hope it will easier for you我希望这对你来说会更容易

    import geopandas as gpd
    import pandas as pd
    from shapely.geometry import Polygon

    # function to return polygon
    def bbox(long0, lat0, lat1, long1):
        return Polygon([[long0, lat0],
                        [long1,lat0],
                        [long1,lat1],
                        [long0, lat1]])

    test = bbox(9.75, 9.75, 10.25, 10.25)

    gpd.GeoDataFrame(pd.DataFrame(['p1'], columns = ['geom']),
         crs = {'init':'epsg:4326'},
         geometry = [test]).to_file('poly.shp')

And here is an implementation of Bruno Carballo's answer that applies it to en entire DataFrame:这是 Bruno Carballo 的答案的实现,将其应用于整个 DataFrame:

import geopandas as gpd
import pandas as pd
from shapely.geometry import Polygon

# function to return polygon
def bbox(vec):
    long0, lat0, lat1, long1 = vec[0], vec[1], vec[2], vec[3]
    return Polygon([[long0, lat0],
                    [long0,lat1],
                    [long1,lat1],
                    [long1, lat0]])

def extentPolygon(df):
    return(
        pd.DataFrame({'geometry' : df[['ext_min_x','ext_min_y','ext_max_y','ext_max_x']].apply(bbox, axis = 1)})
    )


df = pd.DataFrame({'ext_min_x' : [9.75, 9.78], 'ext_max_x' : [10.25, 10.28], 
                    'ext_min_y' : [9.75, 9.78],  'ext_max_y' : [10.25, 10.28]})
df = extentPolygon(df)

After which you can easily turn the resulting DataFrame into a GeoDataFrame:之后,您可以轻松地将生成的 DataFrame 转换为 GeoDataFrame:

df_gp = gdp.GeoDataFrame(df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM