Generate points only in Tokyo using python + shapely

Continuation of the last time http://qiita.com/motokazu/items/aaa5970ab34044237cc7

Last time, I drew a circle with a radius from a specific point and generated a point in it, but if you want to generate it only in a certain area, you can not use a circle.

So, use the Shape file of National Land Numerical Information to generate points in Tokyo.

After opening the Download Page, select JPGIS and select "Administrative Area"-> "Tokyo", the latest status Select 2015. Fill out the questionnaire and download.

Try opening the shapefile with QGIS スクリーンショット 2015-11-02 22.02.42.png

Tokyo is displayed!

Use this shapefile to make dots in Tokyo. As a processed image,

  1. Dot with the previous tool
  2. Read the shape file and check if it is in the shape file
  3. OK if the point is in the shape file

Read the shape file and make it usable Please refer to here. http://stackoverflow.com/questions/7861196/check-if-a-geopoint-with-latitude-and-longitude-is-within-a-shapefile

First of all, install and use Use fiona to read Shapefiles

$ pip install shapely
$ pip install fiona

Load the library

# shape
import fiona
from shapely.geometry import Point,asShape

Extract the shape file and check if the points indicated by Latitude and Longitude are in the shape file.

with fiona.open(shapefile) as fiona_collection:
    shapefile_record = fiona_collection.next()
    shape = asShape(shapefile_record['geometry'])
    
    geolat  = fake.geo_coordinate(center=centerlat , radius=latradius)
    geolong = fake.geo_coordinate(center=centerlong, radius=longradius)
    
    point = Point(geolong, geolat)
        if shape.contains(point):

If you think that contains isn't True at all ... because the shapefile is made up of multiple polygons and fiona_collection.next () is fetching only one partition ... I'm sure.

With reference to this, multiple polygons are fused. That helps a lot! https://sites.google.com/site/qgisnoiriguchi/vector01/09

Combine using QGIS. Select the menu "Vector"-> "Spatial Calculation Tool"-> "Fusion" スクリーンショット 2015-11-02 22.51.18.png

Select the shape to generate, OK スクリーンショット 2015-11-02 22.52.02.png

Became one! スクリーンショット 2015-11-02 22.52.53.png

Try to generate points using this shapefile Apparently the number of contains that are True has changed.

Click here for the final code https://gist.github.com/motokazu/a1cb634aa0d6726039bd

Usage example

$ python genpointsbygeo.my.py --shapefile Tokyo.shp  --samples 100 --radius 50000

Recommended Posts

Generate points only in Tokyo using python + shapely
Generate rounded thumbnails in Python
Generate U distribution in Python
Generate QR code in Python
Translate using googletrans in Python
Using Python mode in Processing
Generate 8 * 8 (64) cubes in Blender Python
GUI programming in Python using Appjar
Precautions when using pit in Python
[Python] Generate QR code in memory
Generate Jupyter notebook ".ipynb" in Python
Try using LevelDB in Python (plyvel)
In the python command python points to python3.8
Using global variables in python functions
Study on Tokyo Rent Using Python (3-2)
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Study on Tokyo Rent Using Python (3-3)
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
GUI creation in python using tkinter 2
Generate a first class collection in Python
How to generate a new loggroup in CloudWatch using python within Lambda
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Generate AWS-S3 signed (time-limited) URLs in Python
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
[FX] Hit oanda-API in Python using Docker
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Automatically generate Python Docstring Comment in Emacs
Log in to Slack using requests in Python
Get Youtube data in Python using Youtube Data API
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Module to generate word N-gram in Python
Develop slack bot in python using chat.postMessage
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Study on Tokyo Rent Using Python (3-1 of 3)
Generate a class from a string in Python
Notes for using python (pydev) in eclipse
Generate C language from S-expressions in Python
Disease classification in Random Forest using Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
Meaning of using DI framework in Python
Generate the Look-and-say Sequence featured in QuizKnock in Python
Create a GIF file using Pillow in Python
How to generate permutations in Python and C ++
Email attachments using your gmail account in python.
Creating numbering process using python in DynamoDB Local Numbering process