0

I am trying to reproject a multi-part shapefile which has the projection EPSG:4326 to the same projection as my data raster, which has EPSG:32633. I thought this would be fairly straightforward to do using geopandas, this is the code I use:

shpfile = dirpath + 'Studyarea_sectors/Studyarea_sectors.shp'
geo = gpd.read_file(shpfile)
nrows = geo.loc[:].shape[0]

for i in range(nrows):
    name = geo.loc[i, 'Id']
    data_proj = geo.loc[[i]].copy()
    data_proj['geometry'] = data_proj['geometry'].to_crs(epsg=32633)
    newgeo.loc[[i]].to_file(dirpath + name + '.shp')

The code makes the new shapefiles with the new projection (EPSG:32633) but when I look at the reprojected coordinates they appear to all have values "Inf":

   Id                                           geometry

0 Colesd POLYGON ((inf inf, inf inf, inf inf, inf inf, ...

I have also tried using pyshp to reproject the shapefile, this is the code I have used:

shpf = shapefile.Reader(shp_folder + 'Studyarea_sectors.shp')
shpf_out = shp_folder + 'Studyarea_sectors_utm.shp'
utm_shp = shapefile.Writer(shpf_out,shapefile.POLYGON)

fields = shpf.fields
utm_fields = utm_shp.fields

for name in fields:
    if type(name) == "tuple":
        continue
    else:
        args = name
        utm_shp.field(*args)

records = shpf.records()
for row in records:
    args = row
    utm_shp.record(*args)

input_proj = Proj(init="epsg:4326")
output_proj = Proj(init="epsg:32633")

geom = shpf.shapes()

for feature in geom:
    poly_list = []

    for coords in feature.points:
        x, y = coords[0], coords[1]
        new_x, new_y = transform(input_proj, output_proj, x, y)
        poly_coord = [float(new_x), float(new_y)]
        poly_list.append(poly_coord)

    utm_shp.poly([poly_list])

utm_shp.close()

But when I open the new file it is also containing only "Inf" in the coordinates of each shape I'm not sure where I have gone wrong - anyone got any idea?

Running geopandas.show_versions() gives me the following output:

python : 3.7.13 (default, Mar 29 2022, 02:18:16) [GCC 7.5.0]

GEOS, GDAL, PROJ INFO

GEOS : 3.8.0 GDAL : 3.0.2 PROJ : 6.2.1

PYTHON DEPENDENCIES

geopandas : 0.9.0 pandas : 1.3.5 fiona : 1.8.13.post1 numpy : 1.21.5 shapely : 1.7.1 rtree : 0.9.7 pyproj : 2.6.1.post1 matplotlib : 3.5.1 mapclassify: 2.4.3 geopy : 2.2.0 psycopg2 : None geoalchemy2: None pyarrow : None pygeos : None

hannahv
  • 1
  • 1
  • Run `geopandas.show_versions()` and edit/post the output in your question. It has useful info for the readers. – swatchai May 28 '23 at 14:21

0 Answers0