如何获得每个国家的边界

时间:2015-11-10 16:32:06

标签: python geolocation

我正在尝试获取世界上每个国家/地区的边界多边形的坐标(纬度和经度),到目前为止我已下载此数据集 (http://thematicmapping.org/downloads/world_borders.php)。

我能够以这种方式检索每个国家的边界​​多边形

import shapefile
sf = shapefile.Reader("TM_WORLD_BORDERS-0.3.sph")
shapes = sf.shapes()

j = 0 # number of the country

lat = []; lon = []
for i in range(len(shapes[j].points)):
    lat.append(shapes[j].points[i][0]);lon.append(shapes[j].points[i][1])

但是,我不明白如何获取与每个多边形关联的国家/地区的名称。

这可能是一个非常简单的问题,但我真的找不到解决方案。

我试着看一下形状的属性,但这个国家的名字似乎并不存在。

我下载的数据集包含一堆其他二进制文件,但我不知道他们是如何编写的,所以我希望有人可能熟悉这个数据集。 shapefile库。

2 个答案:

答案 0 :(得分:0)

对我来说这很有效:

fname = "data/TM_WORLD_BORDERS-0.3/TM_WORLD_BORDERS-0.3.shp"
import shapefile
sf = shapefile.Reader(fname)
shapes = sf.shapeRecords()
shape = shapes[1]
print shape.record
points = shape.shape.points

答案 1 :(得分:0)

我测试了你的代码,看起来经纬度坐标颠倒了。

# -*- coding: utf-8 -*-
"""
Created on Wed Jun  2 21:22:58 2021

@author: yrtst
"""

    import shapefile
    fname = r'D:\222\444\TM_WORLD_BORDERS-0.3.shp'
    sf = shapefile.Reader(fname,resolution='h')
    shapes = sf.shapes()

    j = 29# number of the country

    lat = []; lon = []
    for i in range(len(shapes[j].points)):
        lat.append(shapes[j].points[i][1]);lon.append(shapes[j].points[i][0])

    print(lat[2000])
    print(lon[2000])




    #Guess the countires!

    #First find the approximate range of the coordinates of a country's border. 
    #Then traverse all the border data to find the intersection.

    #Take China's latitude and longitude range as an example.


    import shapefile
    fname = r'D:\222\444\TM_WORLD_BORDERS-0.3.shp'
    sf = shapefile.Reader(fname,resolution='h')
    shapes = sf.shapes()
    j=0
    possible_lat=[]
    possilbe_lon=[]
    while j<246:

        lat = []; lon = []
        for i in range(len(shapes[j].points)):
            lat.append(shapes[j].points[i][1]);lon.append(shapes[j].points[i][0])
            #print(lat)
            #print(lon)
            #ll=[lat,lon]
            #print(lon)


        #the approximate range of the latitude
        for check_lat in lat:
            if 21<check_lat<24 :
                possible_lat.append(j)
                break

        #the approximate range of the longitude
        for check_lon in lon:
            if 130<check_lon<133 :

                possilbe_lon.append(j)
                break
   
        
        lat = []; lon = []
        j+=1    

    #The data that appears in the following two lists at the same time 
    #is the country code we are looking for.
    print(possible_lat)
    print(possilbe_lon)