检查十进制经纬度是否符合使用ETRS89或WGS84的形状

2024-05-14 03:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是来自http://centrodedescargas.cnig.es/CentroDescargas/descargaDir?codSerie=NGMEN&secDescDirCata=9000029recintos_municipales_inspire_canarias_wgs84/recintos_municipales_inspire_canarias_wgs84.shp(伊比利亚半岛)和recintos_municipales_inspire_peninbal_etrs89/recintos_municipales_inspire_peninbal_etrs89.shp(加那利群岛)

recintos_municipales_inspire_canarias_wgs84.shpWGS84中,而recintos_municipales_inspire_peninbal_etrs89/recintos_municipales_inspire_peninbal_etrs89.shpETRS89

这些{}包括西班牙市政当局

我的输入是十进制纬度和经度的坐标(就像使用谷歌地图一样),我想找到每个坐标的形状并记录下来,以便知道它是哪个城市

我的代码是:

import os
from glob import iglob

import shapefile
from shapely.geometry import Point, shape


class Limites:
    def __init__(self):
        self.loaded = False
        self.mum_shape = None
        self.cache = None
        self.municipios = None
        self.shp_glob = "fuentes/fomento/shp/**/recintos*municipales*.shp"
        self.load()

    def load(self):
        self.mum_shape = {}

        for _shp in iglob(self.shp_glob):
            with shapefile.Reader(_shp) as shp:
                for sr in shp.shapeRecords():
                    if sr.shape.points and sr.record and len(sr.record) > 4:
                        natcode = sr.record[4]
                        cod_provincia = natcode[4:6]
                        cod_municipio = natcode[6:11]
                        if cod_municipio.isdigit():
                            # cod_municipio is the code of the municipality
                            self.mum_shape[cod_municipio] = shape(sr.shape)

    def get_mun(self, *args):
        if len(args) == 1:
            lon, lat = args[0]
        else:
            lat, lon = args

        if lat is None or lon is None:
            return None

        # Notice I don't transform my inputs coordinates
        pt = Point(lon, lat)
        for cod, shape in self.mum_shape.items():
            if pt.within(shape):
                self.cache[key] = cod
                return cod

        return None

因此:

api = Limites()
for lat, lon in examples:
    cod = api.get_mun(lat, lon)
    print(cod, lat, lon)

给我:

01002 42.95475500825238 -2.988995546398232
01002 42.96862577694547 -3.022636472021145
01002 43.01779066005873 -3.001256392041849
01002 43.01783568741503 -2.9989983257369985
50297 41.68840665639695 -0.8945899329424668
50297 41.689351183011986 -0.9589016255236483
50297 41.68976237242881 -0.9076274079177915
50297 41.69019415400018 -0.8390439978717974
50297 41.690921990476305 -0.922432326986155
50297 41.69238834082928 -0.8931229785130677
53078 42.2631347430172 -1.6029643041098365
53078 42.26376312876644 -1.5822540065813446
53078 42.27154088788224 -1.5980980328636185
53078 42.2779123426699 -1.5772436099894478
53081 42.7410587024739 -2.267269166087958
53083 42.59430339419461 -2.133990314570195
53083 42.594328861144895 -2.134977234866234
53083 42.61983800152092 -2.104055019644161
38001 28.133993662846912 -16.69357822817092
38001 28.139638024553733 -16.69034791418032
38001 28.185697236251013 -16.688464429796745
38001 28.189354928925457 -16.664676386985626
38001 28.190015481472468 -16.664533830343355
38001 28.191111255764703 -16.66422484905208
38001 28.191757434108634 -16.668227889664106
38001 28.19182683901162 -16.6669760521866
38001 28.19183814632877 -16.668269888310906
38001 28.192262118364244 -16.665322432092907
38001 28.19524516870002 -16.67084908665734
38002 28.128412629492587 -17.255581295691577
38003 28.092813797776806 -17.24746964788173
38004 28.342082282040078 -16.478254250380807
38004 28.352159982147626 -16.430341675149016
38004 28.352604993420737 -16.474145707171267
38004 28.354862772153275 -16.437662467058136
38004 28.355010569096088 -16.43905199442764
38004 28.35504746348884 -16.466925550386925

这看起来很好,但我不知道为什么它会起作用。 我的意思是,你不应该在检查坐标之前转换坐标吗? 我不知道是shapely让这对我有用还是我错过了什么


Tags: importselfnoneifcodlonlatshape