Polygon Driver



Finding out if a certain point is located inside or outside of an area,or finding out if a line intersects with another line or polygon arefundamental geospatial operations that are often used e.g. to selectdata based on location. Such spatial queries are one of the typicalfirst steps of the workflow when doing spatial analysis. Performing aspatial join (will be introduced later) between two spatial datasets isone of the most typical applications where Point in Polygon (PIP) queryis used.

Driver
  1. General Polygon is one of the largest contract facility producing Polygon Profiles in the world. Polygon Configurations have been proven superior to splines and keys for high speed, high torque drives, and close fits. General Polygon Systems, Inc.
  2. Polygon is a gaming website in partnership with Vox Media. Our culture focused site covers games, their creators, the fans, trending stories and entertainment news.

PolyGON is a handy and reliable application that you can use to enhance your songs and bring new sounds to your mixes. It comes with two oscillators, a filter tool, sound delay effects, a reverb.

Paragon

For further reading about PIP and other geometric operations,see Chapter 4.2 in Smith, Goodchild & Longley: Geospatial Analysis - 6th edition.

How to check if point is inside a polygon?¶

Computationally, detecting if a point is inside a polygon is most commonly done using a specific formula called Ray Casting algorithm.Luckily, we do not need to create such a function ourselves forconducting the Point in Polygon (PIP) query. Instead, we can takeadvantage of Shapely’s binary predicatesthat can evaluate the topolocical relationships between geographicalobjects, such as the PIP as we’re interested here.

There are basically two ways of conducting PIP in Shapely:

  1. using a function calledwithin()that checks if a point is within a polygon

  2. using a function calledcontains()that checks if a polygon contains a point

Notice: even though we are talking here about Point in Polygonoperation, it is also possible to check if a LineString or Polygon isinside another Polygon.

Let’s import shapely functionalities and create some points:

Let’s also create a polygon using a list of coordinate-tuples:

  • Let’s check if those points are within the polygon:

Okey, so we can see that the first point seems to be inside that polygonand the other one isn’t.

-In fact, the first point is quite close to close to the center of the polygon as wecan see if we compare the point location to the polygon centroid:

It is also possible to do PIP other way around, i.e. to check ifpolygon contains a point:

Thus, both ways of checking the spatial relationship are identical; contains() is inverse to within() and vice versa.

Which one should you use then? Well, it depends:

  • if you have many points and just one polygon and you try to find outwhich one of them is inside the polygon: You might need to iterate over the points and check one at a time if itis within() the polygon.

  • if you have many polygons and just one point and you want to find outwhich polygon contains the point: You might need to iterate over the polygons until you find a polygon that contains() the point specified (assuming there are no overlapping polygons)

Intersect¶

Another typical geospatial operation is to see if a geometry intersectsor touches another one. Again, there are binary operations in Shapely for checking these spatial relationships:

Polygon
  • intersects(): Two objects intersect if the boundary or interior of one object intersect in any way with the boundary or interior of the other object.

  • touches(): Two objects touch if the objects have at least one point in common and their interiors do not intersect with any part of the other object.

Let’s try these out.

Let’s create two LineStrings:

Let’s see if they intersect

Do they also touch?

Indeed, they do and we can see this by plotting the features together

Thus, the line_b continues from the same node ( (1,1) ) where line_a ends.

However, if the lines overlap fully, they don’t touch due to the spatial relationship rule, as we can see:

Check if line_a touches itself:

It does not. However, it does intersect:

Point in Polygon using Geopandas¶

Next we will do a practical example where we check which of the addresses from the geocoding tutorial are located in Southern district of Helsinki. Let’s start by reading a KML-file PKS_suuralue.kml that has the Polygons for districts of Helsinki Region (data openly available from Helsinki Region Infoshare.

Paragon Driver Apfs

Let’s start by reading the addresses from the Shapefile that we saved earlier.

addressidaddrgeometry
0Ruoholahti, 14, Itämerenkatu, Ruoholahti, Läns...1000Itämerenkatu 14, 00101 Helsinki, FinlandPOINT (24.91556 60.16320)
1Kamppi, 1, Kampinkuja, Kamppi, Eteläinen suurp...1001Kampinkuja 1, 00100 Helsinki, FinlandPOINT (24.93169 60.16902)
2Kauppakeskus Citycenter, 8, Kaivokatu, Keskust...1002Kaivokatu 8, 00101 Helsinki, FinlandPOINT (24.94179 60.16989)
3Hermannin rantatie, Verkkosaari, Kalasatama, S...1003Hermannin rantatie 1, 00580 Helsinki, FinlandPOINT (24.97783 60.18892)
4Hesburger, 9, Tyynenmerenkatu, Jätkäsaari, Län...1005Tyynenmerenkatu 9, 00220 Helsinki, FinlandPOINT (24.92160 60.15665)

Reading KML-files in Geopandas¶

It is possible to read the data from KML-files with GeoPandas in a similar manner as Shapefiles. However, we need to first, enable the KML-driver which is not enabled by default (because KML-files can contain unsupported data structures, nested folders etc., hence be careful when reading KML-files). Supported drivers are managed with fiona.supported_drivers, which is integrated in geopandas. Let’s first check which formats are currently supported:

Polygon Drivers

  • Let’s enable the read and write functionalities for KML-driver by passing 'rw' to whitelist of fiona’s supported drivers:

Let’s check again the supported drivers:

Now we should be able to read a KML file using the geopandas read_file() function.

  • Let’s read district polygons from a KML -file that is located in the>NameDescriptiongeometry0Suur-EspoonlahtiPOLYGON Z ((24.77506 60.10906 0.00000, 24.7766...1Suur-KauklahtiPOLYGON Z ((24.61578 60.17257 0.00000, 24.6155...2Vanha-EspooPOLYGON Z ((24.67576 60.21201 0.00000, 24.6752...3Pohjois-EspooPOLYGON Z ((24.76792 60.26920 0.00000, 24.7699...4Suur-MatinkyläPOLYGON Z ((24.75361 60.16631 0.00000, 24.7537...5KauniainenPOLYGON Z ((24.69075 60.21958 0.00000, 24.6924...6Suur-LeppävaaraPOLYGON Z ((24.79747 60.20827 0.00000, 24.7954...7Suur-TapiolaPOLYGON Z ((24.84436 60.16598 0.00000, 24.8443...8MyyrmäkiPOLYGON Z ((24.82459 60.29025 0.00000, 24.8243...9KivistöPOLYGON Z ((24.94309 60.33845 0.00000, 24.9421...10EteläinenPOLYGON Z ((24.78277 60.09997 0.00000, 24.8197...

Nice, now we can see that we have 23 districts in our area.Let’s quickly plot the geometries to see how the layer looks like:

We are interested in an area that is called Eteläinen (‘Southern’ in English).

Let’s select the Eteläinen district and see where it is located on a map:

NameDescriptiongeometry
0EteläinenPOLYGON Z ((24.78277 60.09997 0.00000, 24.8197...
  • Let’s create a map which shows the location of the selected district, and let’s also plot the geocoded address points on top of the map:

Okey, so we can see that, indeed, certain points are within the selected red Polygon.

Let’s find out which one of them are located within the Polygon. Hence, we are conducting a Point in Polygon query.

First, let’s check that we have shapely.speedups enabled. This module makes some of the spatial queries running faster (starting from Shapely version 1.6.0 Shapely speedups are enabled by default):

  • Let’s check which Points are within the southern Polygon. Notice, that here we check if the Points are within the geometryof the southern GeoDataFrame.

  • We use the .at[0,'geometry'] to parse the actual Polygon geometry object from the GeoDataFrame.

As we can see, we now have an array of boolean values for each row, where the result is Trueif Point was inside the Polygon, and False if it was not.

We can now use this mask array to select the Points that are inside the Polygon. Selecting data with this kind of mask array (of boolean values) is easy by passing the array inside the loc indexer:

Polygon Driver Game

Polygon Driver
addressidaddrgeometry
0Ruoholahti, 14, Itämerenkatu, Ruoholahti, Läns...1000Itämerenkatu 14, 00101 Helsinki, FinlandPOINT (24.91556 60.16320)
1Kamppi, 1, Kampinkuja, Kamppi, Eteläinen suurp...1001Kampinkuja 1, 00100 Helsinki, FinlandPOINT (24.93169 60.16902)
2Kauppakeskus Citycenter, 8, Kaivokatu, Keskust...1002Kaivokatu 8, 00101 Helsinki, FinlandPOINT (24.94179 60.16989)
4Hesburger, 9, Tyynenmerenkatu, Jätkäsaari, Län...1005Tyynenmerenkatu 9, 00220 Helsinki, FinlandPOINT (24.92160 60.15665)
10Rautatientori, 2, Kaivokatu, Keskusta, Kluuvi,...1011Rautatientori 1, 00100 Helsinki, FinlandPOINT (24.93985 60.17038)
30Kampin keskus, 1, Urho Kekkosen katu, Kamppi, ...1031Urho Kekkosen katu 1, 00100 Helsinki, FinlandPOINT (24.93312 60.16909)
31Ruoholahdenkatu, Hietalahti, Kamppi, Eteläinen...1032Ruoholahdenkatu 17, 00101 Helsinki, FinlandPOINT (24.92477 60.16488)
32Easy Cycles, 3, Tyynenmerenkatu, Jätkäsaari, L...1033Tyynenmerenkatu 3, 00220 Helsinki, FinlandPOINT (24.92119 60.15890)
33Oluthuone Kaisla, 4, Vilhonkatu, Kaisaniemi, K...1034Vilhonkatu 4, 00101 Helsinki, FinlandPOINT (24.94709 60.17191)

Paragon Driver

Let’s finally confirm that our Point in Polygon query worked as it should by plotting the points that are within the southern district:

Perfect! Now we only have the (golden) points that, indeed, are inside the red Polygon which is exactly what we wanted!