Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Error extracting vertices from lines #290

Closed
ccastelblanco opened this issue Aug 2, 2023 · 3 comments
Closed

Error extracting vertices from lines #290

ccastelblanco opened this issue Aug 2, 2023 · 3 comments

Comments

@ccastelblanco
Copy link

Hello,

I would like to have your help please with an error I am experiencing when I try to use the function extract_xyz. I am trying to get the coordinates of some fault data to drape the lines over a DEM. After passing the faults gdf and the dem raster to the function extract_xyz like "gg.vector.extract_xyz(gdf=faults, dem=dem, reset_index=False)", the following error appears:

`---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key)
3652 try:
-> 3653 return self._engine.get_loc(casted_key)
3654 except KeyError as err:

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc()

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas_libs\index.pyx:176, in pandas._libs.index.IndexEngine.get_loc()

File pandas_libs\hashtable_class_helper.pxi:2606, in pandas._libs.hashtable.Int64HashTable.get_item()

File pandas_libs\hashtable_class_helper.pxi:2630, in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)
Cell In[63], line 2
1 faults = gdf_f_clip[gdf_f_clip.is_valid]
----> 2 faults_xyz = gg.vector.extract_xyz(gdf=faults, dem=dem, reset_index=False)

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\gemgis\vector.py:1940, in extract_xyz(gdf, dem, minz, maxz, extent, reset_index, drop_index, drop_id, drop_points, drop_level0, drop_level1, target_crs, bbox, remove_total_bounds, threshold_bounds)
1938 # Extracting xyz
1939 if isinstance(dem, rasterio.io.DatasetReader):
-> 1940 gdf = extract_xyz_rasterio(gdf=gdf,
1941 dem=dem,
1942 reset_index=False,
1943 drop_id=False,
1944 drop_index=False,
1945 drop_level0=False,
1946 drop_level1=False,
1947 drop_points=False,
1948 remove_total_bounds=remove_total_bounds,
1949 threshold_bounds=threshold_bounds)
1951 elif isinstance(dem, np.ndarray):
1952 gdf = extract_xyz_array(gdf=gdf,
1953 dem=dem,
1954 extent=extent,
(...)
1961 remove_total_bounds=remove_total_bounds,
1962 threshold_bounds=threshold_bounds)

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\gemgis\vector.py:1298, in extract_xyz_rasterio(gdf, dem, minz, maxz, reset_index, drop_index, drop_id, drop_points, drop_level0, drop_level1, target_crs, bbox, remove_total_bounds, threshold_bounds)
1296 # Extracting X and Y coordinates if they are not present in the GeoDataFrame
1297 if not {'X', 'Y'}.issubset(gdf.columns):
-> 1298 gdf = extract_xy(gdf=gdf,
1299 reset_index=False,
1300 drop_index=False,
1301 drop_id=False,
1302 drop_points=False,
1303 drop_level0=False,
1304 drop_level1=False,
1305 overwrite_xy=False,
1306 target_crs=None,
1307 bbox=None,
1308 remove_total_bounds=remove_total_bounds,
1309 threshold_bounds=threshold_bounds)
1311 # If the CRS of the gdf and the dem are identical, just extract the heights using the rasterio sample method
1312 # NB: for points outside the bounds of the raster, nodata values will be returned
1313 if gdf.crs == dem.crs:

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\gemgis\vector.py:744, in extract_xy(gdf, reset_index, drop_index, drop_id, drop_points, drop_level0, drop_level1, overwrite_xy, target_crs, bbox, remove_total_bounds, threshold_bounds)
742 # Extracting x,y coordinates from line vector data
743 if all(gdf.geom_type == "LineString"):
--> 744 gdf = extract_xy_linestrings(gdf=gdf,
745 reset_index=False,
746 drop_id=False,
747 drop_index=False,
748 drop_points=False,
749 overwrite_xy=overwrite_xy,
750 target_crs=crs,
751 bbox=bbox)
753 # Extracting x,y coordinates from point vector data
754 elif all(gdf.geom_type == "Point"):

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\gemgis\vector.py:480, in extract_xy_linestrings(gdf, reset_index, drop_id, drop_index, drop_points, drop_level0, drop_level1, overwrite_xy, target_crs, bbox)
477 gdf['points'] = [shapely.get_coordinates(geometry=gdf.geometry[i],
478 include_z=True) for i in range(len(gdf))]
479 else:
--> 480 gdf['points'] = [shapely.get_coordinates(geometry=gdf.geometry[i],
481 include_z=False) for i in range(len(gdf))]
483 # Creating DataFrame from exploded columns
484 df = pd.DataFrame(data=gdf).explode('points')

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\gemgis\vector.py:480, in (.0)
477 gdf['points'] = [shapely.get_coordinates(geometry=gdf.geometry[i],
478 include_z=True) for i in range(len(gdf))]
479 else:
--> 480 gdf['points'] = [shapely.get_coordinates(geometry=gdf.geometry[i],
481 include_z=False) for i in range(len(gdf))]
483 # Creating DataFrame from exploded columns
484 df = pd.DataFrame(data=gdf).explode('points')

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\geopandas\geoseries.py:629, in GeoSeries.getitem(self, key)
628 def getitem(self, key):
--> 629 return self._wrapped_pandas_method("getitem", key)

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\geopandas\geoseries.py:622, in GeoSeries._wrapped_pandas_method(self, mtd, *args, **kwargs)
620 def _wrapped_pandas_method(self, mtd, *args, **kwargs):
621 """Wrap a generic pandas method to ensure it returns a GeoSeries"""
--> 622 val = getattr(super(), mtd)(*args, **kwargs)
623 if type(val) == Series:
624 val.class = GeoSeries

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\series.py:1007, in Series.getitem(self, key)
1004 return self._values[key]
1006 elif key_is_scalar:
-> 1007 return self._get_value(key)
1009 if is_hashable(key):
1010 # Otherwise index.get_value will raise InvalidIndexError
1011 try:
1012 # For labels that don't resolve as scalars like tuples and frozensets

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\series.py:1116, in Series._get_value(self, label, takeable)
1113 return self._values[label]
1115 # Similar to Index.get_value, but we do not fall back to positional
-> 1116 loc = self.index.get_loc(label)
1118 if is_integer(loc):
1119 return self._values[loc]

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\indexes\multi.py:2812, in MultiIndex.get_loc(self, key)
2809 return mask
2811 if not isinstance(key, tuple):
-> 2812 loc = self._get_level_indexer(key, level=0)
2813 return _maybe_to_slice(loc)
2815 keylen = len(key)

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\indexes\multi.py:3160, in MultiIndex._get_level_indexer(self, key, level, indexer)
3157 return slice(i, j, step)
3159 else:
-> 3160 idx = self._get_loc_single_level_index(level_index, key)
3162 if level > 0 or self._lexsort_depth == 0:
3163 # Desired level is not sorted
3164 if isinstance(idx, slice):
3165 # test_get_loc_partial_timestamp_multiindex

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\indexes\multi.py:2752, in MultiIndex._get_loc_single_level_index(self, level_index, key)
2750 return -1
2751 else:
-> 2752 return level_index.get_loc(key)

File c:\Users\carli\anaconda3\envs\gemgis\lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key)
3653 return self._engine.get_loc(casted_key)
3654 except KeyError as err:
-> 3655 raise KeyError(key) from err
3656 except TypeError:
3657 # If we have a listlike key, _check_indexing_error will raise
3658 # InvalidIndexError. Otherwise we fall through and re-raise
3659 # the TypeError.
3660 self._check_indexing_error(key)

KeyError: 1`

Additionally, I tried to run the related cell in the example "42_draping_linestrings_over_dem_in_pyvista.ipynb" but an error results as well.

I appreciate your kind help to fix this.

Kind regards,

Carlos

@AlexanderJuestel
Copy link
Collaborator

Hello @ccastelblanco,

Would it be possible for you to provide the data for us? Then I can check directly where the error is :)

Cheers
Alex

@ccastelblanco
Copy link
Author

Thank you! Yes, sure. Here I attach the files:

test_files.zip

I appreciate your help!

@AlexanderJuestel
Copy link
Collaborator

Hey @ccastelblanco,

I think the error was that you also have MultiLineStrings in your data. That is why GemGIS could not handle it. We currently support only GeoDataFrames/Shape Files with one geometry type. A solution that worked for me now is the following. Here, the MultiLineStrings are exploded into single LineStrings and the index was reset.

faults = faults.explode().reset_index(drop=True)
faults_xyz = gg.vector.extract_xyz(faults, dem)

Let me know if that worked. I will also convert your issue to a discussion :)

@cgre-aachen cgre-aachen locked and limited conversation to collaborators Aug 3, 2023
@AlexanderJuestel AlexanderJuestel converted this issue into discussion #291 Aug 3, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants