Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

geopandas bug ?

1,448 views
Skip to first unread message

Xristos Xristoou

unread,
Jan 14, 2017, 6:20:48 AM1/14/17
to
i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ?



geopandas code :

from geopandas import gpd
import geopandas
points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc
polys = geopandas.GeoDataFrame.from_file('polygons.shp')
pointInPoly = gpd.sjoin(points, polys, how='left',op='within')

error :

Traceback (most recent call last):
File "/home/username/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in <module>
pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin
r_idx = np.concatenate(idxmatch.values)
ValueError: need at least one array to concatenate

and if i change the imports with the some code :

import geopandas
import pandas as pd
import geopandas as gpd
from geopandas import GeoDataFrame, read_file
from geopandas.tools import sjoin
from shapely.geometry import Point, mapping,shape
import pandas as gpd

i take that error :

pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
AttributeError: 'module' object has no attribute 'sjoin'


any idea why ?

Peter Otten

unread,
Jan 14, 2017, 8:43:10 AM1/14/17
to
Xristos Xristoou wrote:

> i want to create a simple spatial joing using geopandas but i thing so
> geopandas has bug ?

Have you tried the examples on <http://geopandas.org/mergingdata.html>?
Do they work? If yes, inspect your data, does it have the same format?

> geopandas code :
>
> from geopandas import gpd
> import geopandas
> points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc
> polys = geopandas.GeoDataFrame.from_file('polygons.shp')
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
>
> error :
>
> Traceback (most recent call last):
> File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py",
> line 7, in <module>
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
> File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py",
> line 57, in sjoin
> r_idx = np.concatenate(idxmatch.values)
> ValueError: need at least one array to concatenate
>
> and if i change the imports with the some code :
>
> import geopandas
> import pandas as pd
> import geopandas as gpd
> from geopandas import GeoDataFrame, read_file
> from geopandas.tools import sjoin
> from shapely.geometry import Point, mapping,shape
> import pandas as gpd
>
> i take that error :
>
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
> AttributeError: 'module' object has no attribute 'sjoin'
>
>
> any idea why ?

My crystal ball says that either points or polys is empty ;)


Xristos Xristoou

unread,
Jan 14, 2017, 9:06:42 AM1/14/17
to
is not empty and yes i have two shapefiles from qgis.what is the error ?

Peter Otten

unread,
Jan 14, 2017, 9:30:48 AM1/14/17
to
Xristos Xristoou wrote:

> Τη Σάββατο, 14 Ιανουαρίου 2017 - 3:43:10 μ.μ. UTC+2, ο χρήστης Peter Otten
> έγραψε:
>> Xristos Xristoou wrote:
>>
>> > i want to create a simple spatial joing using geopandas but i thing so
>> > geopandas has bug ?
>>
>> Have you tried the examples on <http://geopandas.org/mergingdata.html>?
>> Do they work? If yes, inspect your data, does it have the same format?

Looks like you chose to ignore the hard part.

>> > geopandas code :
>> >
>> > from geopandas import gpd
>> > import geopandas
>> > points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson
>> > etc polys = geopandas.GeoDataFrame.from_file('polygons.shp')
>> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
>> >
>> > error :
>> >
>> > Traceback (most recent call last):
>> > File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py",
>> > line 7, in <module>
>> > pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
>> > File
>> > "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py",
>> > line 57, in sjoin
>> > r_idx = np.concatenate(idxmatch.values)
>> > ValueError: need at least one array to concatenate

>> > any idea why ?
>>
>> My crystal ball says that either points or polys is empty ;)
>
> is not empty and yes i have two shapefiles from qgis.

Can I download those files somewhere?

> what is the error ?

No idea. Without those files I cannot run your code.

Xristos Xristoou

unread,
Jan 14, 2017, 9:37:16 AM1/14/17
to
yes i sent you

duncan smith

unread,
Jan 14, 2017, 9:38:39 AM1/14/17
to
On 14/01/17 11:18, Xristos Xristoou wrote:
> i want to create a simple spatial joing using geopandas but i thing so geopandas has bug ?
>
>
>
> geopandas code :
>
> from geopandas import gpd
> import geopandas
> points = geopandas.GeoDataFrame.from_file('points.shp') # or geojson etc
> polys = geopandas.GeoDataFrame.from_file('polygons.shp')
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
>
> error :
>
> Traceback (most recent call last):
> File "/home/sarantis/testshapely/sumpointsinsidepolygon/testgeo.py", line 7, in <module>
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
> File "/usr/local/lib/python2.7/dist-packages/geopandas/tools/sjoin.py", line 57, in sjoin
> r_idx = np.concatenate(idxmatch.values)
> ValueError: need at least one array to concatenate
>
> and if i change the imports with the some code :
>
> import geopandas
> import pandas as pd
> import geopandas as gpd
> from geopandas import GeoDataFrame, read_file
> from geopandas.tools import sjoin
> from shapely.geometry import Point, mapping,shape
> import pandas as gpd
>
> i take that error :
>
> pointInPoly = gpd.sjoin(points, polys, how='left',op='within')
> AttributeError: 'module' object has no attribute 'sjoin'
>
>
> any idea why ?
>


import geopandas as gpd
import pandas as gpd

Does pandas have an attribute 'sjoin'?

Duncan

Xristos Xristoou

unread,
Jan 14, 2017, 9:59:09 AM1/14/17
to

Xristos Xristoou

unread,
Jan 14, 2017, 10:00:18 AM1/14/17
to
i dont know i follow detais i am newbie

Peter Otten

unread,
Jan 14, 2017, 11:01:39 AM1/14/17
to
It looks like there are no intersections in your data.
With the proviso that I've learned about the library only today I think you
should get an empty result set rather than the ValueError.
Here's a way to reproduce the error (?) with the data provided in the
project:

import geopandas
from geopandas import gpd

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
cities = gpd.read_file(gpd.datasets.get_path('naturalearth_cities'))
countries = world[['geometry', 'name']]

def find(items, name):
# didn't find the idomatic way quickly, so
for index, n in enumerate(items["name"]):
if n == name:
return items[index:index+1]
raise ValueError

berlin = find(cities, "Berlin")
paris = find(cities, "Paris")
germany = find(countries, "Germany")

print(gpd.sjoin(berlin, germany))
print(gpd.sjoin(paris, germany)) # ValueError

$ python demo.py
geometry name_left index_right \
175 POINT (13.39960276470055 52.52376452225116) Berlin 41

name_right
175 Germany

[1 rows x 4 columns]
Traceback (most recent call last):
File "demo.py", line 20, in <module>
print(gpd.sjoin(paris, germany)) # ValueError
File "/home/peter/virt/geopandas/lib/python3.4/site-
packages/geopandas/tools/sjoin.py", line 57, in sjoin
r_idx = np.concatenate(idxmatch.values)
ValueError: need at least one array to concatenate
$

I suggest that you file a bug report.


Xristos Xristoou

unread,
Jan 14, 2017, 11:11:34 AM1/14/17
to
Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 i use instersection on QGIS ad work fine

Peter Otten

unread,
Jan 14, 2017, 11:33:54 AM1/14/17
to
Xristos Xristoou wrote:

>> I suggest that you file a bug report.
>
> Mr.Peter Otten do you see my shapefiles ?have instersection 100 to 100 i
> use instersection on QGIS ad work fine

Yes, I downloaded the zipfile at

> https://www.dropbox.com/s/2693nfi248z0y9q/files.zip?dl=0 with the

and when I ran your code I got the very error that you saw. There are many
NaN values in your data, so if it works elsewhere perhaps the data is
corrupted in some way. I'm sorry I cannot help you any further.

Good luck!

Xristos Xristoou

unread,
Jan 14, 2017, 11:41:43 AM1/14/17
to
one more question,i have a idea what is wrong,but if my code work how to export spatial join "pointInPoly" to new shapefile ?

Peter Otten

unread,
Jan 14, 2017, 12:31:25 PM1/14/17
to
You can find an object's methods in the interactive interpreter with dir()

>>> dir(pointInPoly)
['T', '_AXIS_ALIASES', '_AXIS_IALIASES', '_AXIS_LEN', '_AXIS_NAMES',
'_AXIS_NUMBERS', '_AXIS_ORDERS', '_AXIS_REVERSED', '_AXIS_SLICEMAP',

<snip>

'rmod', 'rmul', 'rotate', 'rpow', 'rsub', 'rtruediv', 'save', 'scale',
'select', 'set_geometry', 'set_index', 'set_value', 'shape', 'shift',
'simplify', 'sindex', 'skew', 'sort', 'sort_index', 'sortlevel', 'squeeze',
'stack', 'std', 'sub', 'subtract', 'sum', 'swapaxes', 'swaplevel',
'symmetric_difference', 'tail', 'take', 'to_clipboard', 'to_crs', 'to_csv',
'to_dense', 'to_dict', 'to_excel', 'to_file', 'to_gbq', 'to_hdf', 'to_html',
'to_json', 'to_latex', 'to_msgpack', 'to_panel', 'to_period', 'to_pickle',
'to_records', 'to_sparse', 'to_sql', 'to_stata', 'to_string',
'to_timestamp', 'to_wide', 'total_bounds', 'touches', 'translate',
'transpose', 'truediv', 'truncate', 'tshift', 'type', 'tz_convert',
'tz_localize', 'unary_union', 'union', 'unstack', 'update', 'values', 'var',
'where', 'within', 'xs']

OK, that's quite a lot, but to_file() seems to be a good candidate. Let's
see what it does:

>>> help(pointInPoly.to_file)
Help on method to_file in module geopandas.geodataframe:

to_file(filename, driver='ESRI Shapefile', schema=None, **kwargs) metod of
geopandas.geodataframe.GeoDataFrame instance
Write this GeoDataFrame to an OGR data source

A dictionary of supported OGR providers is available via:
>>> import fiona
>>> fiona.supported_drivers

Parameters
----------
filename : string
File path or file handle to write to.
driver : string, default 'ESRI Shapefile'
The OGR format driver used to write the vector file.
schema : dict, default None
If specified, the schema dictionary is passed to Fiona to
better control how the file is written.

The *kwargs* are passed to fiona.open and can be used to write
to multi-layer data, store data within archives (zip files), etc.

Looks good, run it:

>>> pointInPoly.to_file("point_in_poly")

No error. Does it round-trip?

>>> pointInPoly == gpd.GeoDataFrame.from_file("point_in_poly")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3/dist-packages/pandas/core/ops.py", line 875, in f
return self._compare_frame(other, func, str_rep)
File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 2860, in
_compare_frame
raise ValueError('Can only compare identically-labeled '
ValueError: Can only compare identically-labeled DataFrame objects

Ouch, unfortunately not. Upon further inspection:

>>> pip.columns
Index(['geometry', 'index_righ', 'name_left', 'name_right'], dtype='object')
>>> pointInPoly.columns
Index(['geometry', 'name_left', 'index_right', 'name_right'],
dtype='object')

Looks like column names are either corrupted or limited to 10 characters by
default. Again I don't know how to overcome this, but as a special service
here's the first hit for 'shp file column name limit' on a popular search
engine:

http://gis.stackexchange.com/questions/15784/how-to-bypass-10-character-limit-of-field-name-in-shapefiles

I'm out of this now.


duncan smith

unread,
Jan 14, 2017, 1:09:53 PM1/14/17
to
You import geopandas as gpd, then import pandas as gpd. So maybe you
think gpd refers to geopandas while it actually refers to pandas. I
don't know geopandas or pandas, but you should check your imports.

Duncan

Xristos Xristoou

unread,
Jan 14, 2017, 1:30:10 PM1/14/17
to
@Duncan i see that and i remove line with pandas ans no change
0 new messages