首页 > 解决方案 > 在地理上将一个国家划分为等距的点(Python)

问题描述

我正在尝试将巴拿马地图拆分为等距的纬度、经度坐标,并使用 numpy 创建将它们分开的 linspace,基于底部左、右下、左上和右上的纬度。为此,我使用了嵌套的 for 循环。

问题是,尽管在最终字典中获得了正确数量的唯一纬度,但我只得到了一个唯一经度。你能帮我纠正循环中的错误,以便输入最终数据帧的最终字典给出 646 以响应

grid_centroid.long.nunique()

非常感谢!

import pandas as pd
import numpy as np

bottomLeft = (7.239013, -82.94546842973114)
bottomRight = (7.239013, -77.177479)
topLeft = (9.62079503922844, -82.94546842973114)
topRight = (9.62079503922844, -77.177479)

df = pd.read_csv('sites_unique.csv')


cols = np.linspace(bottomLeft[1], bottomRight[1], num=276)
rows = np.linspace(bottomLeft[0], topLeft[0], num=646)
df['col'] = np.searchsorted(cols, df['Average of lat'], 'right')
df['row'] = np.searchsorted(rows, df['Average of long'], 'right')

grid_dict = {'lat':[],'long':[]}

for l in range(275):
    lat_index_init = l
    for i in range(645):
        lon_init_index = i
        grid_dict['lat'].append(rows[lat_index_init])
        grid_dict['long'].append(cols[lon_index_init])            
        i + 1        
    l + 1
    i = 0

grid_centroid = pd.DataFrame(grid_dict)



标签: pythonpandasnumpy

解决方案


快速修复,想通了,它实际上非常简单

bottomLeft = (7.239013, -82.94546842973114)
bottomRight = (7.239013, -77.177479)
topLeft = (9.62079503922844, -82.94546842973114)
topRight = (9.62079503922844, -77.177479)

cols = np.linspace(bottomLeft[1], bottomRight[1], num=646)
rows = np.linspace(bottomLeft[0], topLeft[0], num=275)

grid_dict = {'lat':[],'long':[]}

for l in range(275):
    lat_index_init = l
    for i in range(645):
        grid_dict['lat'].append(rows[l])
        grid_dict['long'].append(cols[i])            
        i + 1        
    l + 1
    i = 0

产生期望的结果。我希望这可以帮助其他努力创建地理空间网格的人

一切顺利


推荐阅读