ssb-sgis 1.0.5__py3-none-any.whl → 1.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,9 @@ from geopandas import GeoSeries
11
11
  from shapely.geometry import Point
12
12
 
13
13
  from ..geopandas_tools.general import _push_geom_col
14
+ from ..geopandas_tools.general import make_edge_coords_cols
15
+ from ..geopandas_tools.general import make_edge_wkt_cols
16
+ from ..geopandas_tools.geometry_types import make_all_singlepart
14
17
 
15
18
 
16
19
  def make_node_ids(
@@ -35,7 +38,7 @@ def make_node_ids(
35
38
  Note:
36
39
  The lines must be singlepart linestrings.
37
40
  """
38
- gdf = gdf.explode(index_parts=False).explode(index_parts=False)
41
+ gdf = make_all_singlepart(gdf, ignore_index=True)
39
42
 
40
43
  if wkt:
41
44
  gdf = make_edge_wkt_cols(gdf)
@@ -92,112 +95,3 @@ def make_node_ids(
92
95
  gdf = _push_geom_col(gdf)
93
96
 
94
97
  return gdf, nodes
95
-
96
-
97
- def make_edge_coords_cols(gdf: GeoDataFrame) -> GeoDataFrame:
98
- """Get the wkt of the first and last points of lines as columns.
99
-
100
- It takes a GeoDataFrame of LineStrings and returns a GeoDataFrame with two new
101
- columns, source_coords and target_coords, which are the x and y coordinates of the
102
- first and last points of the LineStrings in a tuple. The lines all have to be
103
-
104
- Args:
105
- gdf (GeoDataFrame): the GeoDataFrame with the lines
106
-
107
- Returns:
108
- A GeoDataFrame with new columns 'source_coords' and 'target_coords'
109
- """
110
- try:
111
- gdf, endpoints = _prepare_make_edge_cols_simple(gdf)
112
- except ValueError:
113
- gdf, endpoints = _prepare_make_edge_cols(gdf)
114
-
115
- coords = [(geom.x, geom.y) for geom in endpoints.geometry]
116
- gdf["source_coords"], gdf["target_coords"] = (
117
- coords[0::2],
118
- coords[1::2],
119
- )
120
-
121
- return gdf
122
-
123
-
124
- def make_edge_wkt_cols(gdf: GeoDataFrame) -> GeoDataFrame:
125
- """Get coordinate tuples of the first and last points of lines as columns.
126
-
127
- It takes a GeoDataFrame of LineStrings and returns a GeoDataFrame with two new
128
- columns, source_wkt and target_wkt, which are the WKT representations of the first
129
- and last points of the LineStrings
130
-
131
- Args:
132
- gdf (GeoDataFrame): the GeoDataFrame with the lines
133
-
134
- Returns:
135
- A GeoDataFrame with new columns 'source_wkt' and 'target_wkt'
136
- """
137
- try:
138
- gdf, endpoints = _prepare_make_edge_cols_simple(gdf)
139
- except ValueError:
140
- gdf, endpoints = _prepare_make_edge_cols(gdf)
141
-
142
- wkt_geom = [
143
- f"POINT ({x} {y})" for x, y in zip(endpoints.x, endpoints.y, strict=True)
144
- ]
145
- gdf["source_wkt"], gdf["target_wkt"] = (
146
- wkt_geom[0::2],
147
- wkt_geom[1::2],
148
- )
149
-
150
- return gdf
151
-
152
-
153
- def _prepare_make_edge_cols(
154
- lines: GeoDataFrame, strict: bool = False
155
- ) -> tuple[GeoDataFrame, GeoDataFrame]:
156
- lines = lines.loc[lines.geom_type != "LinearRing"]
157
-
158
- if not (lines.geom_type == "LineString").all():
159
- multilinestring_error_message = (
160
- "MultiLineStrings have more than two endpoints. "
161
- "Try shapely.line_merge and/or explode() to get LineStrings. "
162
- "Or use the Network class methods, where the lines are prepared correctly."
163
- )
164
- if (lines.geom_type == "MultiLinestring").any():
165
- raise ValueError(multilinestring_error_message)
166
- else:
167
- raise ValueError(
168
- "You have mixed geometries. Only lines are accepted. "
169
- "Try using: to_single_geom_type(gdf, 'lines')."
170
- )
171
-
172
- geom_col = lines._geometry_column_name
173
-
174
- # some LineStrings are in fact rings and must be removed manually
175
- boundary = lines[geom_col].boundary
176
- circles = boundary.loc[boundary.is_empty]
177
- lines = lines[~lines.index.isin(circles.index)]
178
-
179
- endpoints = lines[geom_col].boundary.explode(ignore_index=True)
180
-
181
- if len(lines) and len(endpoints) / len(lines) != 2:
182
- raise ValueError(
183
- "The lines should have only two endpoints each. "
184
- "Try splitting multilinestrings with explode.",
185
- lines[geom_col],
186
- )
187
-
188
- return lines, endpoints
189
-
190
-
191
- def _prepare_make_edge_cols_simple(
192
- lines: GeoDataFrame,
193
- ) -> tuple[GeoDataFrame, GeoDataFrame]:
194
- """Faster version of _prepare_make_edge_cols."""
195
- endpoints = lines[lines._geometry_column_name].boundary.explode(ignore_index=True)
196
-
197
- if len(lines) and len(endpoints) / len(lines) != 2:
198
- raise ValueError(
199
- "The lines should have only two endpoints each. "
200
- "Try splitting multilinestrings with explode."
201
- )
202
-
203
- return lines, endpoints