第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

通過(guò)openstreetmap API獲取國(guó)家邊界

通過(guò)openstreetmap API獲取國(guó)家邊界

我想畫(huà)一張歐洲地圖。為此,我需要將國(guó)家輪廓作為多邊形。為此,我想使用 openstreetmap API。我嘗試了一下,overpy但我對(duì)單個(gè)國(guó)家/地區(qū)的結(jié)果需要 10 分鐘才能執(zhí)行,而且看起來(lái)不正確(似乎這些方法不適合在一起)。到目前為止我的代碼:import matplotlib.pyplot as pltimport overpyapi = overpy.Overpass()result=api.query("area['name:en'='Denmark']->.country;rel['name:en'='Denmark']['type'='boundary']['admin_level'='2'];(way(r)['maritime' != 'yes'](40,-10,70,80);way(area.country)['natural'='coastline'](40,-10,70,80););out geom;")x=[]y=[]i=0for way in result.ways:    print(f"way {i} of {len(result.ways)}")    i=i+1    for node in way.get_nodes(True):        x.append(float(node.lon))        y.append(float(node.lat))plt.plot(x, y,label=str(way.id))plt.show()我走在正確的軌道上還是有更好的解決方案?謝謝!
查看完整描述

1 回答

?
jeck貓

TA貢獻(xiàn)1909條經(jīng)驗(yàn) 獲得超7個(gè)贊

我認(rèn)為這沒(méi)關(guān)系,只需使用點(diǎn)而不是線。對(duì)于長(zhǎng)時(shí)間運(yùn)行我也沒(méi)有解決方案。

https://img1.sycdn.imooc.com//64f7269f00018c0405790421.jpg

import matplotlib.pyplot as plt


import overpy

api = overpy.Overpass()


result=api.query("area['name:en'='Denmark']->.country;rel['name:en'='Denmark']['type'='boundary']['admin_level'='2'];(way(r)['maritime' != 'yes'](40,-10,70,80);way(area.country)['natural'='coastline'](40,-10,70,80););out geom;")


x=[]

y=[]

i=0

for way in result.ways:

    print(f"way {i} of {len(result.ways)}")

    if 'natural' in way.tags and way.tags['natural']=='coastline' and len(way.get_nodes(True))>0: #just a test

        i=i+1

        for node in way.get_nodes(True):

            print (f'lon: {float(node.lon):3.4f}; lat: {float(node.lat):3.4f}')

            x.append(float(node.lon))

            y.append(float(node.lat))

plt.plot(x, y, 'o',label=str(way.id))

plt.show()

由于多邊形而編輯:


import json

import matplotlib.pyplot as plt

import overpy



def getData():

    api = overpy.Overpass()

    result = api.query("area['name:en'='Denmark']->.country;rel['name:en'='Denmark']['type'='boundary']['admin_level'='2'];(way(r)['maritime' != 'yes'](40,-10,70,80);way(area.country)['natural'='coastline'](40,-10,70,80););out geom;")


    x = []

    y = []

    i = 0

    for way in result.ways:

        print(f"way {i} of {len(result.ways)}")

        # just a test

        if 'natural' in way.tags and way.tags['natural'] == 'coastline' and len(way.get_nodes(True)) > 0:

            i = i+1

            x1 = []

            y1 = []

            for node in way.get_nodes(True):

                print(

                    f'lon: {float(node.lon):3.4f}; lat: {float(node.lat):3.4f}')

                x1.append(float(node.lon))

                y1.append(float(node.lat))

            x.append(x1)

            y.append(y1)


    xy = [x, y]

    with open('data.txt', 'w') as f:

        json.dump(xy, f)



def readDate():

    with open('data.txt', 'r') as f:

        return json.load(f)



getData()

data = readDate()



last = None

first = None

d = []

k = [[], []]

m = []

while(len(data[0]) > 0):

    if last == None and first == None:   # Make sure that there are no "ways" at the beginning or end that match the line.

        last = [data[0][0][-1], data[1][0][-1]] # Get first and last point of a new line

        first = [data[0][0][0], data[1][0][0]]

        k[0] = k[0] + data[0][0] # Start the new line

        k[1] = k[1] + data[1][0]

        data[0].pop(0) # Drop the way

        data[1].pop(0) 

    for j in range(0, len(data[0])): # Check all lines


        if first == [data[0][j][-1], data[1][j][-1]]: # If the first ...

            print(f'First {first[0]}; {first[1]}')

            k = [data[0][j] + k[0], data[1][j] + k[1]]

            first = [data[0][j][0], data[1][j][0]]

            data[0].pop(j)

            data[1].pop(j)

            break


        if last == [data[0][j][0], data[1][j][0]]: # or the last point continue the current line

            print(f'Last  {last[0]}; {last[1]}')

            k = [k[0] + data[0][j], k[1] + data[1][j]] # Add the segment to the new line

            last = [data[0][j][-1], data[1][j][-1]] # Set the point new last point

            data[0].pop(j) # Drop the way

            data[1].pop(j)

            break


        if j == len(data[0])-1: # When the for-loop reaches the end, there is no "way" that continue the line

            m.append(k)

            k = [[], []]

            first = None

            last = None


    if len(data[0]) == 1: # If the last remaining line is a small island, just add it.

        k = [data[0][0], data[1][0]]

        m.append(k)

        data[0].pop(0)

        data[1].pop(0)



for i in range(0, len(m)):

    plt.plot(m[i][0], m[i][1], label=f'Denmark')


plt.show()

該算法以盡可能創(chuàng)建多邊形的方式排列 API 中的“方式”。

https://img1.sycdn.imooc.com//64f726b20001e8bc05930419.jpg

查看完整回答
反對(duì) 回復(fù) 2023-09-05
  • 1 回答
  • 0 關(guān)注
  • 414 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)