-1

I am working with some geodata. I want to get a point object from the latitude and longitude that I already have. My code looks as follows:

for i in range(len(map_polygon)):
    x = map_polygon.at[i, 'lon'] 
    y = map_polygon.at[i, 'lat']
    map_polygon.at[i, 'point'] = Point(x, y)

However, I am getting the following error: RecursionError: maximum recursion depth exceeded in comparison

What am I doing wrong? The length of the data frame is only about 20k rows.

Thank you!

PS I have attached the whole error message below:

map_polygon.at[i, 'point'] = Point(x, y)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 2167, in __setitem__
    return super().__setitem__(key, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 2118, in __setitem__
    self.obj._set_value(*key, value=value, takeable=self._takeable)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/frame.py", line 3278, in _set_value
    self.loc[index, col] = value
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 692, in __setitem__
    iloc._setitem_with_indexer(indexer, value, self.name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1635, in _setitem_with_indexer
    self._setitem_with_indexer_split_path(indexer, value, name)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1720, in _setitem_with_indexer_split_path
    self._setitem_single_column(loc, value, pi)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/indexing.py", line 1813, in _setitem_single_column
    ser._mgr = ser._mgr.setitem(indexer=(pi,), value=value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 568, in setitem
    return self.apply("setitem", indexer=indexer, value=value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/managers.py", line 427, in apply
    applied = getattr(b, f)(**kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 967, in setitem
    return self.astype(dtype).setitem(indexer, value)
  [Previous line repeated 977 more times]
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/internals/blocks.py", line 966, in setitem
    dtype, _ = maybe_promote(np.array(value).dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geometry/point.py", line 111, in array_interface
    if self.is_empty:
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geometry/base.py", line 699, in is_empty
    return (self._geom is None) or bool(self.impl['is_empty'](self))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/predicates.py", line 25, in __call__
    return self.fn(this._geom)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/shapely/geos.py", line 583, in errcheck_predicate
    if result == 2:
RecursionError: maximum recursion depth exceeded in comparison
Michael Butscher
  • 10,028
  • 4
  • 24
  • 25

1 Answers1

0

When adding a geometry column, looping and setting each value individually is usually a bad idea (slow) so you probably want to do it like this:

map_polygon['point'] = [Point(p) for p in zip(df.lon, df.lat)]
Bruno Carballo
  • 1,156
  • 8
  • 15
  • In OP's example, `Point(...)` takes 2 arguments, here you've given it a single argument as a tuple. Should probably be `[Point(*p) for p in zip(df.lon, df.lat)]` or `[Point(lon, lat) for lon, lat in zip(df.lon, df.lat)]` – Cameron Riddell Jun 01 '21 at 16:49
  • 1
    According to shapely's documentation, Point takes either a tuple or positional arguments: "The Point constructor takes positional coordinate values or point tuple parameters. " source = https://shapely.readthedocs.io/en/stable/manual.html#points So both work, and passing the tuple means less typing – Bruno Carballo Jun 01 '21 at 21:12