10

I am new to GIS. I am trying to map out wards for the city of Milwuakee using shapefiles found on their county website county website. I am following Converting projected coordinates to lat/lon using Python? with some success. My code is:

from pyproj import Proj, transform
# wisconsing EPSG:32054
# epsg:4326 is for the entire world, wgs 84...not obvious
inProj = Proj(init='epsg:32054')
outProj = Proj(init='epsg:4326')
x1,y1 = 2560131.496875003, 406816.434375003
x2,y2 = transform(inProj,outProj,x1,y1)
print(x2,y2)

with output,

-65.70220967836329 43.08590211722421

Problem is this is wrong. The lon/lat for Milwaukee is -87.863984 and 42.920816.

Secondly, how can I do this programmatically for the entire shapefile. I would like to plot this into basemap. When I try to follow Shapefile projection for Matplotlib Basemap I get an error

Code:

with fiona.open("ward2012/ward.shp") as shp:
    ori = Proj(init='epsg:32054' ),
    dest= Proj(init='EPSG:4326',preserve_units=True)
    with fiona.open('ward2012/MKE_wards_lat_lon.shp', 'w', 'ESRI Shapefile', shp.schema.copy(), crs=from_epsg(4326))as output:
        for point in shp:
            x,y =  point['geometry']['coordinates']
            point['geometry']['coordinates'] = transform(ori, dest,x,y)
            output.write(point)

error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-139-a5079ab39f99> in <module>()
      4     with fiona.open('ward2012/MKE_wards_lat_lon.shp', 'w', 'ESRI Shapefile', shp.schema.copy(), crs=from_epsg(4326))as output:
      5         for point in shp:
----> 6             x,y =  point['geometry']['coordinates']
      7             point['geometry']['coordinates'] = transform(ori, dest,x,y)
      8             output.write(point)

ValueError: not enough values to unpack (expected 2, got 1)

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
superhero
  • 111
  • 1
  • 4

1 Answers1

13

At the first question, 'epsg:32054' code has feet units. For this reason, it is necessary to use 'preserve_units=True' as parameter in inProj = Proj(init='epsg:32054') line. Now, next code works well:

from pyproj import Proj, transform
# wisconsing EPSG:32054
# epsg:4326 is for the entire world, wgs 84...not obvious
inProj = Proj(init='epsg:32054', preserve_units=True)
outProj = Proj(init='epsg:4326')
x1,y1 = 2560131.496875003, 406816.434375003
x2,y2 = transform(inProj,outProj,x1,y1)
print (x2,y2)
(-87.9028568836077, 43.09691266312185)

At the second question, ward.shp is a polygon shapefile; not a point shapefile. In this case, you can use geopandas module for reprojection. My proposed code is (with my particular path):

import geopandas as gpd

tmp = gpd.GeoDataFrame.from_file('/home/zeito/pyqgis_data/ward2012/ward.shp')

tmpWGS84 = tmp.to_crs({'proj':'longlat', 'ellps':'WGS84', 'datum':'WGS84'})

tmpWGS84.to_file('/home/zeito/pyqgis_data/ward2012/wardWGS84.shp')

In next image, you can see reprojected shapefile (wardWGS84.shp) and the point (-87.9028568836077, 43.09691266312185) before considered at Map Canvas of QGIS:

enter image description here

xunilk
  • 29,891
  • 4
  • 41
  • 80