9

I was trying to execute this code:

# Load geojson files
country = gpd.read_file('svn_buffered.geojson')

train_shp = "large_training_aoi.geojson"
test_shp = "large_test_aoi.shp"
train_polyg = gpd.read_file(train_shp)
test_polyg = gpd.read_file(test_shp)

# reproject the polygons 
target_crs = CRS.UTM_33N
country = country.to_crs(target_crs.pyproj_crs())
train_polyg = train_polyg.to_crs(target_crs.pyproj_crs())
test_polyg = test_polyg.to_crs(target_crs.pyproj_crs())


fontdict = {'family': 'monospace', 'weight': 'normal', 'size': 11}

fig, ax = plt.subplots(figsize=(20, 20))
test_polyg.plot(ax=ax, facecolor='w', edgecolor='r', alpha=0.5) #test area in red
train_polyg.plot(ax=ax, facecolor='w', edgecolor='g', alpha=0.5) # train area in green
country.plot(ax=ax, facecolor='w', edgecolor='b', alpha=0.5)
ax.set_title('Selected train AOI (in green) and test AOI (in red)');
plt.axis('off')

I tried to use the following syntax:

test_polyg = test_polyg.to_crs(epsg=32632)

and I got the error each time but only for the test data.

ValueError                                Traceback (most recent call last)
<ipython-input-20-323e17a794e3> in <module>()
    11 country = country.to_crs(target_crs.pyproj_crs())
    12 train_polyg = train_polyg.to_crs(target_crs.pyproj_crs())
---> 13 test_polyg = test_polyg.to_crs(target_crs.pyproj_crs())
    14 
    15 

1 frames
/usr/local/lib/python3.6/dist-packages/geopandas/geoseries.py in to_crs(self, crs, epsg)
   420         if self.crs is None:
   421             raise ValueError(
--> 422                 "Cannot transform naive geometries.  "
   423                 "Please set a crs on the object first."
   424             )

ValueError: Cannot transform naive geometries.  Please set a crs on the object first.

Any idea on how can I fix it?

1 Answer 1

13

There is a high chance your data do not have an associated CRS at the loading time.

So you first need to actually set your GeoDataFrame CRS before re-projecting your data, such as the following:

your_gdf.crs = "EPSG:4326" (or whatever EPSG code your data are expressed in)

From the documentation:

There are two relevant operations for projections: setting a projection and re-projecting.

Setting a projection may be necessary when for some reason geopandas has coordinate data (x-y values), but no information about how those coordinates refer to locations in the real world. Setting a projection is how one tells geopandas how to interpret coordinates. If no CRS is set, geopandas geometry operations will still work, but coordinate transformations will not be possible and exported files may not be interpreted correctly by other software.

Be aware that most of the time you don’t have to set a projection. Data loaded from a reputable source (using the geopandas.read_file() command) should always include projection information. You can see an objects current CRS through the GeoSeries.crs attribute.

Source: https://geopandas.org/en/stable/docs/user_guide/projections.html

3
  • When I tried train_polyg.crs I got <Geographic 2D CRS: EPSG:4326> but when I did the same test_polyg.crs I got nothing. Is it possible that the file is corrupted?
    – Rim Sleimi
    Commented May 19, 2020 at 17:10
  • Read carefully the documentation. It's totally normal; this means your CRS is actually not set! Hence, you have to do it yourself. If you know the projection system of your data, set it using the aforementioned command. If you don't know, try to figure it out by reading the metadata, the API documentation, or any information at the source where you get your data, or ask the person who gave it to you. Commented May 19, 2020 at 17:39
  • Okey I'll try that, thanks!
    – Rim Sleimi
    Commented May 19, 2020 at 18:04

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.