Internal / external judgment with Python: Obtain the city / town / village name from the latitude / longitude information of any point

Thing you want to do

I made a program to get the name of the city, town, and village of this point only from the latitude and longitude information of any point. There are two points: reading the shapefile and judging the inside and outside of the point.

What to prepare

I used two types of modules, pyshp and Sympy (installed with pip). -Pyshp (used to read shp files) ・ Sympy (used for internal / external judgment) ・ Shapefile of the boundary of municipalities (Reference: https://www.esrij.com/products/japan-shp/)

Create and run a program

The source code is as follows.

import shapefile
from sympy.geometry import Point, Polygon
#
LONG=140.0
LAT=36.5
RPOINT=Point(LONG,LAT)  #➀ Judgment target point setting
#
src=shapefile.Reader('.\\shp\\japan_ver821.shp',encoding='SHIFT-JIS')  #File reading
SRS=src.shapeRecords()  #➁ shp data reading
for srs in SRS:
    shp=srs.shape   #➁ Acquisition of feature information
    rec=srs.record  #➁ Attribute information acquisition
    box=shp.bbox    #➁ Get the rectangle that surrounds each object
    #
    #➂ If the target point is within a rectangle, perform a more detailed internal / external judgment
    if box[0]<LONG<box[2] and box[1]<LAT<box[3]:  
        pnt=shp.points  #➃ Get coordinate information of each object
        points=[]       #➃ Extract nodes of each object
        for pp in pnt:   
            points.append((pp[0],pp[1]))
        poly=Polygon(*points)   #➃ Create sympy polygon data
        #
        if poly.encloses_point(RPOINT):  #➃ Conduct internal / external judgment
            print(rec)                   #➄ Output city / town / village information
            break

The following processes are performed in order. ① Judgment target point setting ② Read shp data ③ Check if there is a judgment target point within the rectangular range of each object ④ ③ If there is a target point within the range, carry out a more detailed internal / external judgment ⑤ Output attribute information when internal / external judgment is successful

Execution result
Record #469: ['Tochigi Prefecture', '', '', '', 'Utsunomiya City']

I was able to get the name of the city.

※important point※

This method doesn't seem to work if the shp data contains donut-shaped polygons consisting of two or more closed curves. In such a case, we would appreciate it if you could refer to the following. https://qiita.com/Pooh-A/items/6b4fcb15a790f6ee6e79

Recommended Posts

Internal / external judgment with Python: Obtain the city / town / village name from the latitude / longitude information of any point
Iterator that can scan forward from the middle of the sequence
Existence from the viewpoint of Python
Obtain the direct product of sequences of indefinite length within a specific sequence.
Internal / external judgment with Python: Obtain the city / town / village name from the latitude / longitude information of any point
Obtain statistics etc. from the extracted sample
Learning notes from the beginning of Python 1
Omit BOM from the beginning of the string
Learning notes from the beginning of Python 2
Obtain location information (latitude and longitude) from the address. Geocode in Python ~ Geocoder and pydams ~
Extract the band information of raster data with python
How to get the pixel value of the point from the satellite image by specifying the latitude and longitude
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
List of disaster dispatches from the Sapporo City Fire Department [Python]
Find the distance from latitude and longitude (considering the roundness of the earth).
Find the waypoint from latitude and longitude (considering the roundness of the earth).
Create a compatibility judgment program with the random module of python.
PhytoMine-I tried to get the genetic information of plants with Python
[Completed version] Try to find out the number of residents in the town from the address list with Python