0

I have GeoDataFrame that I am trying to plot in relation to 4 reference points. I'm creating the reference points as part of a separate GeoDataFrame, combining them and then trying to plot the result. I am using the following code:

import pandas
import geopandas as gpd
from geopandas import GeoDataFrame
from shapely.geometry import Point

def dfTogpdf(df):
    geometry = [Point(xy) for xy in zip(df.Lon, df.Lat)]
    df = df.drop(['Lon', 'Lat'], axis=1)
    crs = {'init': 'epsg:4326'}

    return GeoDataFrame(df, crs=crs, geometry=geometry)


habitat = gpd.read_file('desktop/species_19377/species_19377.shp')

latitude = [90, -90, 0, 0]
longitude = [0, 0, -179, 180]
frame = dfTogpdf(pandas.DataFrame({'Lat': latitude, 'Lon': longitude}))

framed = gpd.sjoin(frame, habitat,  how="inner", op='intersects')
framed.geometry.plot()

However this gives me the following error: TypeError: invalid type comparison

I am able to plot habitat and frame without any issue, but after they have been combined, I am left with an empty GeoDataFrame and not able to plot the result. Is there an extra step I need to take to plot my combined GeoDataFrames?

species_19377.shp

Traceback:

C:\Users\Alec\Anaconda3\lib\site-packages\pandas\core\ops.py:798: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  result = getattr(x, name)(y)
Traceback (most recent call last):

  File "<ipython-input-255-31bfbe140734>", line 1, in <module>
    runfile('C:/Users/Alec/test.py', wdir='C:/Users/Alec')

  File "C:\Users\Alec\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\Alec\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Alec/test.py", line 29, in <module>
    framed.geometry.plot()

  File "C:\Users\Alec\Anaconda3\lib\site-packages\geopandas\geoseries.py", line 253, in plot
    return plot_series(self, *args, **kwargs)

  File "C:\Users\Alec\Anaconda3\lib\site-packages\geopandas\plotting.py", line 273, in plot_series
    poly_idx = np.asarray((geom_types == 'Polygon')

  File "C:\Users\Alec\Anaconda3\lib\site-packages\pandas\core\ops.py", line 861, in wrapper
    res = na_op(values, other)

  File "C:\Users\Alec\Anaconda3\lib\site-packages\pandas\core\ops.py", line 800, in na_op
    raise TypeError("invalid type comparison")

TypeError: invalid type comparison

habitat.head():

             ID_NO        BINOMIAL  PRESENCE  ORIGIN COMPILER  YEAR  \
index_right                                                           
0            19377  Ratufa bicolor         1       1     IUCN  2008   
1            19377  Ratufa bicolor         1       1     IUCN  2008   
2            19377  Ratufa bicolor         1       1     IUCN  2008   
3            19377  Ratufa bicolor         1       1     IUCN  2008   
4            19377  Ratufa bicolor         1       1     IUCN  2008   

                                                      CITATION SOURCE  \
index_right                                                             
0            IUCN (International Union for Conservation of ...   None   
1            IUCN (International Union for Conservation of ...   None   
2            IUCN (International Union for Conservation of ...   None   
3            IUCN (International Union for Conservation of ...   None   
4            IUCN (International Union for Conservation of ...   None   

            DIST_COMM    ISLAND SUBSPECIES SUBPOP TAX_COMMEN  \
index_right                                                    
0                None   Con Son       None   None       None   
1                None      None       None   None       None   
2                None      Bali       None   None       None   
3                None      None       None   None       None   
4                None  Billiton       None   None       None   

                        LEGEND  SEASONAL  \
index_right                                
0            Extant (resident)         1   
1            Extant (resident)         1   
2            Extant (resident)         1   
3            Extant (resident)         1   
4            Extant (resident)         1   

                                                      geometry  
index_right                                                     
0            POLYGON ((106.6621000000001 8.752335593000055,...  
1            POLYGON ((113.1460000000001 -8.175164407999944...  
2            POLYGON ((115.2932000000001 -8.102764407999928...  
3            POLYGON ((97.17740000000003 2.226135593000038,...  
4            POLYGON ((107.8088000000001 -2.535364406999975...  

framed.head():

Empty GeoDataFrame
Columns: [geometry, index_right, ID_NO, BINOMIAL, PRESENCE, ORIGIN, COMPILER, YEAR, CITATION, SOURCE, DIST_COMM, ISLAND, SUBSPECIES, SUBPOP, TAX_COMMEN, LEGEND, SEASONAL]
Index: []
9
  • Edit your question and provide the error traceback.
    – user2856
    Commented Apr 25, 2018 at 4:40
  • Your shapefile gives an error when opening (in QGIS/ArcGIS): species_19377.shp is not a valid or recognized data source. Your data may be corrupted. Commented Apr 25, 2018 at 15:28
  • This is my first time working with shapefles, and I actually don't have any GIS software. However, as mentioned in the question I am able to plot the data from it with geopandas and interact with it fully through the module. It's the result of sjoin that I can't plot
    – asheets
    Commented Apr 25, 2018 at 17:03
  • Oo. you shared just .shp file. Shapefile consists of different files in same name. Could you add outputs of habitat.head() and framed.head() to your question. Commented Apr 25, 2018 at 23:13
  • @KadirŞahbaz I've altered my script to specify the directory instead of the .shp file, however all output seems the same.
    – asheets
    Commented Apr 26, 2018 at 2:15

1 Answer 1

1

You are trying to plot an empty GeoSeries, as evidenced by your post:

>>> framed.head():
Empty GeoDataFrame
Columns: [geometry, index_right, ID_NO, BINOMIAL, PRESENCE, ORIGIN, COMPILER, YEAR, CITATION, SOURCE, DIST_COMM, ISLAND, SUBSPECIES, SUBPOP, TAX_COMMEN, LEGEND, SEASONAL]
Index: []

The TypeError is being triggered by the fact that this is an empty GeoDataFrame; the plotting code did not expect this edge case. This was a known bug and has been fixed, but the geopandas project hasn't had a new release since then. If you really need it fixed, you will have to install bleeding-edge geopandas from the source on GitHub.

But on the other hand, maybe you're looking in the wrong place. Perhaps you didn't expect an empty result from the spatial join?

1
  • Right, I did not expect an empty result. I have edited the question to reflect this.
    – asheets
    Commented Apr 26, 2018 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.