Error: float() argument must be a string or a number, not 'Polygon' when trying to find points in polygons
up vote
0
down vote
favorite
I have a list of named polygons:
import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.path as mpltPath
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 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')
I am trying to find out what points are in what polygons and add a column to the point dataframe with 'True' or 'False'
I've previously been shown the methods here which show some ways of quickly doing this and have got the script:
point = points['geometry']
path = mpltPath.Path(df['geometry'])
points['inside'] = path.contains_points(point)
but I get the error: float() argument must be a string or a number, not 'Polygon'
How do I fix this?
Alternatively I have been trying this method:
points['inside'] =
for geo1 in df['geometry']:
for geo2 in points['geometry']:
if geo1.contains(geo2):
points['inside'].append('True')
however here I also get an error: Length of values des not match length of index
Any help with either of these would be greatly appreciated!
python python-3.x pandas matplotlib
add a comment |
up vote
0
down vote
favorite
I have a list of named polygons:
import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.path as mpltPath
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 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')
I am trying to find out what points are in what polygons and add a column to the point dataframe with 'True' or 'False'
I've previously been shown the methods here which show some ways of quickly doing this and have got the script:
point = points['geometry']
path = mpltPath.Path(df['geometry'])
points['inside'] = path.contains_points(point)
but I get the error: float() argument must be a string or a number, not 'Polygon'
How do I fix this?
Alternatively I have been trying this method:
points['inside'] =
for geo1 in df['geometry']:
for geo2 in points['geometry']:
if geo1.contains(geo2):
points['inside'].append('True')
however here I also get an error: Length of values des not match length of index
Any help with either of these would be greatly appreciated!
python python-3.x pandas matplotlib
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a list of named polygons:
import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.path as mpltPath
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 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')
I am trying to find out what points are in what polygons and add a column to the point dataframe with 'True' or 'False'
I've previously been shown the methods here which show some ways of quickly doing this and have got the script:
point = points['geometry']
path = mpltPath.Path(df['geometry'])
points['inside'] = path.contains_points(point)
but I get the error: float() argument must be a string or a number, not 'Polygon'
How do I fix this?
Alternatively I have been trying this method:
points['inside'] =
for geo1 in df['geometry']:
for geo2 in points['geometry']:
if geo1.contains(geo2):
points['inside'].append('True')
however here I also get an error: Length of values des not match length of index
Any help with either of these would be greatly appreciated!
python python-3.x pandas matplotlib
I have a list of named polygons:
import pandas as pd
import geopandas as gp
from shapely.geometry import Polygon
from shapely.geometry import Point
import matplotlib.path as mpltPath
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 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')
I am trying to find out what points are in what polygons and add a column to the point dataframe with 'True' or 'False'
I've previously been shown the methods here which show some ways of quickly doing this and have got the script:
point = points['geometry']
path = mpltPath.Path(df['geometry'])
points['inside'] = path.contains_points(point)
but I get the error: float() argument must be a string or a number, not 'Polygon'
How do I fix this?
Alternatively I have been trying this method:
points['inside'] =
for geo1 in df['geometry']:
for geo2 in points['geometry']:
if geo1.contains(geo2):
points['inside'].append('True')
however here I also get an error: Length of values des not match length of index
Any help with either of these would be greatly appreciated!
python python-3.x pandas matplotlib
python python-3.x pandas matplotlib
asked Nov 21 at 14:17
tom91
839
839
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I didn't get the "true/false column", but this should do the job:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output:
0 (box, [a])
1 (cone, )
2 (triangle, [b])
dtype: object
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
Oh, got it. Then this:points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)
– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I didn't get the "true/false column", but this should do the job:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output:
0 (box, [a])
1 (cone, )
2 (triangle, [b])
dtype: object
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
Oh, got it. Then this:points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)
– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
add a comment |
up vote
1
down vote
accepted
I didn't get the "true/false column", but this should do the job:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output:
0 (box, [a])
1 (cone, )
2 (triangle, [b])
dtype: object
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
Oh, got it. Then this:points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)
– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I didn't get the "true/false column", but this should do the job:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output:
0 (box, [a])
1 (cone, )
2 (triangle, [b])
dtype: object
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I didn't get the "true/false column", but this should do the job:
points.apply(lambda row: (row['id'], list(map(lambda e: e[0], list(filter(lambda p: p[1].contains(row['geometry']), df.values))))), axis=1)
For each point you get the polygons that contains it, output:
0 (box, [a])
1 (cone, )
2 (triangle, [b])
dtype: object
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Nov 21 at 15:18
Federico Pucci
513
513
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Federico Pucci is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
Oh, got it. Then this:points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)
– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
add a comment |
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
Oh, got it. Then this:points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)
– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
Thank you for this. The 'True, False column' would basically be a column that is added to the points dataframe that would have true next to a point if it fell within either polygon a or b. This is because in the actual data set I have 1000s of points to check within 100s of polygons and don't want to be adding a column manually
– tom91
Nov 21 at 15:37
1
1
Oh, got it. Then this:
points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)– Federico Pucci
Nov 21 at 15:49
Oh, got it. Then this:
points['inside'] = points.geometry.map(lambda x: True if reduce(lambda a,b: a+b, list(map(lambda p: p.contains(x), df.geometry.values))) else False)– Federico Pucci
Nov 21 at 15:49
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
YES! Thats got it, thank you so much!
– tom91
Nov 21 at 16:07
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414065%2ferror-float-argument-must-be-a-string-or-a-number-not-polygon-when-trying%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown