1

I have a list of named polygons:

import pandas as pd
import geopandas as gp
df = gp.GeoDataFrame([['a',Polygon([(1, 0), (1, 1), (2,2), (1,2)])],
                     ['b',Polygon([(1, 1), (2,2), (3,1)])]],
                     columns = ['name','geometry'])
df = gp.GeoDataFrame(df, geometry = 'geometry')

and a list of named points:

points = gp.GeoDataFrame( [['box', Point(1.5, 1.75)],
                          ['cone', Point(3.0,2.0)],
                        ['triangle', Point(2.5,1.25)]],
                     columns=['id', 'geometry'], 
                     geometry='geometry')

Currently, I am running a for loop over these points and polygons to see which point falls within which polygon and returning there names and Ids to a list loc like so:

loc = []

for geo1, name in zip(df['geometry'], df['name']):
    for geo2, id in zip(points['geometry'], points['id']):
        if geo1.contains(geo2):
            loc.append([id, name]) 

Now what I want to try and do is alter the loop so it adds a column to the points dataframe called 'inside' and returns 'True' if the point is in a polygon and 'False' if it isn't.

I've tried:

points['inside'] = ''
for geo1 in df['geometry']:
    for geo2 in points['geometry']:
        if geo1.contains(geo2):
            points['inside'].append('True')

but it doesn't work

How can I best do this?

sorry if there is a very basic answer that I have missed.

Its been suggested below that this might be a duplicate of another question, however the one that is linked does not refer to adding the results to a column and whilst the Matplotlib methodology may be faster, when I run the example script provided I get the error float() argument must be a string or a number, not 'zip'

tom91
  • 685
  • 7
  • 24
  • 1
    Possible duplicate of [What's the fastest way of checking if a point is inside a polygon in python](https://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python) – Yuca Nov 21 '18 at 12:56
  • @Yuca I don't think this is a duplicate as that question doesn't relate to adding the results to a column in the dataframe. Also when I try the code for matplotlib I get an error: float() argument must be a string or a number, not 'zip' – tom91 Nov 21 '18 at 13:09
  • 2
    Hi Tom, while the previous answer does not give you the answer in the format that you want/need, it does give you the answer for the bigger problem, which is 'the point is inside a polygon or not'. If you have an error implementing an approved answer then most likely thing is that you have a mistake – Yuca Nov 21 '18 at 13:12
  • Hi Yuca, I would like to try and make their solution work as it is meant to be faster, which is always useful, however I don't think their code works anymore as I copy and pasted the exact code to test it out. – tom91 Nov 21 '18 at 13:19
  • just ran the code, works fine :) good luck – Yuca Nov 21 '18 at 13:22
  • Thanks. Could the reason it isn't working be that that answer is in python 2.7 and I'm in python 3? – tom91 Nov 21 '18 at 13:38
  • @tom91 I am using Python3.6... let me know if you still have the problem... – Moshe Slavin Dec 24 '18 at 11:58

1 Answers1

1

You are trying to append to a string...

Just change the line points['inside'] = '' to points['inside'] = []

points['inside'] = []
for geo1 in df['geometry']:
    for geo2 in points['geometry']:
        if geo1.contains(geo2):
            points['inside'].append('True')

This works for me...

Hope you find this helpful!

Moshe Slavin
  • 5,127
  • 5
  • 23
  • 38
  • Hey that makes a lot of sense! however when I run this I get the error: 'Length of values does not match length of index' – tom91 Nov 21 '18 at 12:54
  • 1
    @tom91 hi did you ever figure it out? do you still need help? – Moshe Slavin Dec 31 '18 at 13:35
  • Hi, I know why the error is occuring but I don't know how to fix it! – tom91 Jan 10 '19 at 11:06
  • Hi @tom91, can you show your new code? what is the error you are getting? – Moshe Slavin Jan 10 '19 at 11:44
  • I've tried running the above code and the error is still 'Length of values does not match length of index'. This is because it returns three values whilst there are only two columns in points. How would I aggregate the output of the ablve for loop to produce counts of 'True', so the point that has 2 Trues is asigned a value of 2 and the point with 1 True is asigned a value of 1? – tom91 Jan 10 '19 at 11:49
  • @tom91 That's a good question... you may want to rewrite the question or write a new one... +1 – Moshe Slavin Jan 10 '19 at 12:03
  • Thank you, I have written a new question relating to [counting the number of intersects a polygon has with other polygons](https://stackoverflow.com/questions/54127731/how-can-i-count-the-number-of-polygons-a-shape-intersects). – tom91 Jan 10 '19 at 12:20