matplotlib / basemap

Plot on map projections (with coastlines and political boundaries) using matplotlib
MIT License
775 stars 392 forks source link

readshapefile() raises incorrect error msg regarding shpproj and shapelib #285

Open pwgerman opened 8 years ago

pwgerman commented 8 years ago

When plotting a shapefile using readshapefile()

m.readshapefile(shapefile_path, 'roads')

I received this error message:

ValueError: shapefile must have lat/lon vertices - it looks like this one has vertices in map projection coordinates. You can convert the shapefile to geographic coordinates using the shpproj utility from the shapelib tools (http://shapelib.maptools.org/shapelib-tools.html)

However, it appears that the specified module "shapelib" is no longer maintained or available. To add to the confusion, there is a completely different module that also goes by the name "shapelib" available on pypi and github, but that does not contain the functionality suggested above.

In regard to a suggested solution. I found the fix on this site to be useful for converting the shapefile and getting past this readshapefile() error. http://fascicul.us/convert-display-shapefile-matplotlib.html

However, this conversion sometimes leads to a new error:

ValueError: readshapefile can only handle 2D shape types

Which I have yet to find a way to resolve

guziy commented 8 years ago

Try to reproject with QGIS.. I agree it's a bit annoying ))

http://gis.stackexchange.com/questions/35590/how-to-reproject-a-vector-layer-in-qgis

Maybe cartopy can handle projected shapefiles, can anyone confirm please?

Cheers

WeatherGod commented 8 years ago

I know that geopandas has a .to_crs() method: http://geopandas.org/user.html#GeoSeries.to_crs, which, annoyingly isn't the same as the CRS objects available in Cartopy, IIRC.

On Wed, Apr 13, 2016 at 1:12 PM, Huziy Oleksandr (Sasha) < notifications@github.com> wrote:

Try to reproject with QGIS.. I agree it's a bit annoying ))

Maybe cartopy can handle projected shapefiles, can anyone confirm please?

Cheers

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/matplotlib/basemap/issues/285#issuecomment-209549842

micahcochran commented 8 years ago

It would be good if the readshapefile function were able to automatically reproject coordinates from a projected coordinate system into WGS84 lat/long (or lon/lat for that matter).

It is probably meaning this shapelib the C library, which hasn't had a release since 2012. That might mean the project has reach perfection :wink:. shapelib has ended up in the code of GDAL and MapServer.

guziy's suggestions of using QGIS to reproject is good. OGR's ogr2ogr using the -t_srs parameter shouldn't be too bad to use either.

pwgerman commented 8 years ago

Hi, Thanks for the feedback. I tried reprojecting the datafile which I got from here: http://apps.sfgov.org/datafiles/view.php?file=sfgis/stclines_streets.zip

I tried conversion in QGIS as you suggested. I also tried converting it with ogr2ogr. Neither of these worked.

In the end I found a different shapefile that works for my purpose from MapZen https://mapzen.com/data/metro-extracts/

Mapzen offers many formats for download. There are three file types I downloaded from them.

I found that IMPOSM.SHP gives files that work directly with basemap.

An alternative format, OSM2PGSQL.SHP, gives this value error when passed to readshapefile. ValueError: could not convert string to float: ****************

I was also able to convert the OSM-XML file from MapZen into a shapefile that works with basemap. I converted it to shapefile using the GoeConverter website.
http://geoconverter.hsr.ch/
This conversion takes a long time to process, so it's not ideal as the IMPOSM.SHP. however, it makes a nice work around if only OSM files are available. (When uploading to GeoConverter, it's important to send the OSM file as a ZIP.)

nishadhka commented 7 years ago

Hi, This error was getting resolved by using geopandas and setting the shape file crs to null as follows

import geopandas as gpd
stateshp = gpd.read_file('shapefile.shp')
#print stateshp1.crs
stateshp1.crs={}
stateshp1.to_file('shapefile1.shp', driver='ESRI Shapefile')

Using the resultant shape file resolves the error.

shalevale commented 4 years ago

I just want to add (for anyone who can help in the future) that I resolve this problem converting my shapefile through this web: https://mygeodata.cloud/