Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Must pass either crs or epsg #12

Closed
sukruburakcetin opened this issue Apr 25, 2023 · 12 comments
Closed

ValueError: Must pass either crs or epsg #12

sukruburakcetin opened this issue Apr 25, 2023 · 12 comments
Labels
bug Something isn't working

Comments

@sukruburakcetin
Copy link

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[11], line 2
      1 shapefile = 'segment_gpu_new.shp'
----> 2 sam.tiff_to_vector(mask, shapefile)

File ~\Desktop\segment-geospatial\samgeo\samgeo.py:172, in SamGeo.tiff_to_vector(self, tiff_path, output, simplify_tolerance, **kwargs)
    169         i["geometry"] = i["geometry"].simplify(tolerance=simplify_tolerance)
    171 gdf = gpd.GeoDataFrame.from_features(fc)
--> 172 gdf.set_crs(epsg=src.crs.to_epsg(), inplace=True)
    173 gdf.to_file(output, **kwargs)

File ~\Anaconda3\envs\segment-geospatial\lib\site-packages\geopandas\geodataframe.py:1279, in GeoDataFrame.set_crs(self, crs, epsg, inplace, allow_override)
   1277 else:
   1278     df = self
-> 1279 df.geometry = df.geometry.set_crs(
   1280     crs=crs, epsg=epsg, allow_override=allow_override, inplace=True
   1281 )
   1282 return df

File ~\Anaconda3\envs\segment-geospatial\lib\site-packages\geopandas\geoseries.py:1031, in GeoSeries.set_crs(self, crs, epsg, inplace, allow_override)
   1029     crs = CRS.from_epsg(epsg)
   1030 else:
-> 1031     raise ValueError("Must pass either crs or epsg.")
   1033 if not allow_override and self.crs is not None and not self.crs == crs:
   1034     raise ValueError(
   1035         "The GeoSeries already has a CRS which is not equal to the passed "
   1036         "CRS. Specify 'allow_override=True' to allow replacing the existing "
   1037         "CRS without doing any transformation. If you actually want to "
   1038         "transform the geometries, use 'GeoSeries.to_crs' instead."
   1039     )
ValueError: Must pass either crs or epsg.

Hello everyone, both the mask that is created and tiff have been projected with 4326, WGS 84 coordinate system. It can be easily seen on the map with their current projection without on the fly action. The mask that was created after sam generate function has 4326 coordinate system. However, when I try to use tiff_to_vector or tiff_to_package functions, I got no crs or epsg is set error. How to pass crs or epsg into these functions?

I implemented the notebook tutorial code with my projected tiff exactly as it is described.
Thanks in advance.

@sukruburakcetin sukruburakcetin added the bug Something isn't working label Apr 25, 2023
@sukruburakcetin
Copy link
Author

sam_geo1

I used raster to polygon geoprocessing tool in order to see how good the result is overlayed.

sam_geo2

This is the geospatial reference information screen that shows the mask that is created with the sam-generate wgs84 coordinate system.

sam_geo3
And this is the original tiff file that is used for mask generation. It also has wgs84 coordinate system as described in its geospatial reference information.

By the way, I would like to say huge thanks for creating this amazing module open source, thank you so much for your amazing work. @giswqs You are the real MVP in GIS.

I would like to say sorry if my workflow is not correct and flagged this as a bug. Regards.

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

What bbox did you use?

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

Can you provide complete source code that can reproduce the issue?

@sukruburakcetin
Copy link
Author

I didn't pass any bbox value since I thought the clipped region that I derivated from the inclusive aerial image has already bbox regions inside of its metadata.

I didn't use this:

if m.user_roi_bounds() is not None:
    bbox = m.user_roi_bounds()
else:
    bbox = [-95.3704, 29.6762, -95.368, 29.6775]

and this function:
tms_to_geotiff(output=image, bbox=bbox, zoom=20, source='Satellite')

This is my notebook:
sam_geo4

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

Can you share the input geotiff for debugging?

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

It appears that the inference runs successfully. You can use the following function directly to debug the issue.

https://samgeo.gishub.org/samgeo/#samgeo.samgeo.SamGeo.tiff_to_vector

  def tiff_to_vector(tiff_path, output, simplify_tolerance=None, **kwargs):
      """Convert a tiff file to a gpkg file.

      Args:
          tiff_path (str): The path to the tiff file.
          output (str): The path to the vector file.
          simplify_tolerance (_type_, optional): The simplify tolerance. Defaults to None.
      """
      import geopandas as gpd
      import rasterio
      from rasterio import features

      with rasterio.open(tiff_path) as src:
          band = src.read()

          mask = band != 0
          shapes = features.shapes(band, mask=mask, transform=src.transform)

      fc = [
          {"geometry": shapely.geometry.shape(shape), "properties": {"value": value}}
          for shape, value in shapes
      ]
      if simplify_tolerance is not None:
          for i in fc:
              i["geometry"] = i["geometry"].simplify(tolerance=simplify_tolerance)

      gdf = gpd.GeoDataFrame.from_features(fc)
      gdf.set_crs(epsg=src.crs.to_epsg(), inplace=True)
      gdf.to_file(output, **kwargs)

@sukruburakcetin
Copy link
Author

Can you share the input geotiff for debugging?

Do you mind if I ask how to share the sample data with you? Which vendor do you prefer, I can send it with an uploaded file link(can share it here or right into your mailbox). The system doesn't allow data of more than 25 MB, and my sample data is around ~100MB. I refrain from choosing a method that is not suitable for you. I want to contribute even if this sample data will improve your workflow.

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

You can upload it to me via this link.

@sukruburakcetin
Copy link
Author

Okay, with the inference function you have provided, I was able to create a shape vector file with WKID code 3857 without any complications(it is a different projection so you cannot overlay it properly). However, when I try it with the code 4326, the shapefile was created with polygon attributes but you cannot show data on the map viewer.

I have tried a bunch of other epsg codes, so you can easily see output on the viewer but it cannot be overlayed on the real tiff data due to their projections do not match. They all work but in a way of is not expected.

This is how I debugged it:
sam_geo6
And this is the output:
sam_geo5

All of them work but their end results don't match(it is expected). The only one which is not worked: is the attempt with code 4326. The shapefile was created but cannot be seen.

Do you mind if I request to try it out with the sample I provided via your link? Thank you so much for your patience and efforts, dear Wu.

@giswqs
Copy link
Member

giswqs commented Apr 25, 2023

I think the problem is your input dataset. From the image extent numbers, it is clearly those numbers are not lat/lon. They are likely epsg:3857. But the CRS shows that it is WGS 1984, which is wrong. So my question is how did you get that dataset? The CRS was not assigned properly.

image

image

The correct one should look like this.
image

@sukruburakcetin
Copy link
Author

Thank you so much, dear Wu.

The problem is solved by changing the data's geospatial coordinate reference system into GCS_WGS_1984(4326) from pseudo-Mercator(3857), so extend parameters are converted into decimal degrees and the inference code worked flawlessly.
sam_geo7

It is right that there is a problem with the main sample tiff, I derived it from our database(which is shown and used in [here](https://sehirharitasi.ibb.gov.tr/ as "2022 Uydu" in the layer selection section). The sample tiff was ESRGAN applied version of raw data(It was the project in which I enhanced raw data with super-resolution techniques). There could be an issue when mosaicking tile files into a raster file.
sam_geo8

However, bear in mind: The function in the sample notebook still asks for crs for being set by the user while the inference function runs great if you set the crs in it.

shapefile = 'segment_gpu_new_decimal_degrees.shp'
sam.tiff_to_vector(mask, shapefile)

@giswqs giswqs mentioned this issue Apr 26, 2023
@giswqs
Copy link
Member

giswqs commented Apr 26, 2023

This bug has been fixed in v0.3.0. Please update the package and try again.

@giswqs giswqs closed this as completed Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants