pysgn.utils#
- sample_points(n: int, *, bbox: Sequence[float] | None = None, polygon: Polygon | MultiPolygon | None = None, sampler: Callable[[Generator, int], ndarray] | None = None, random_state: int | None = None, crs: Any | None = None, max_attempts: int = 1000000) GeoDataFrame[source]#
Sample point geometries within a spatial domain.
Returns a geometry-only
GeoDataFramewith exactlynpoint geometries in thegeometrycolumn.Domain definition:
bboxonly: sample in(xmin, ymin, xmax, ymax)bounds.polygononly: sample in polygon area.both: sample in
polygon.intersection(bbox_polygon).at least one of
bboxorpolygonmust be provided.
Sampling:
sampler is None: uniform candidate generation over the final domain.sampler is callable: user-defined candidate generation withsampler(rng, k) -> array-likeof shape(k, 2), followed by rejection against the final domain.
CRS handling:
CRS is set only from explicit
crsinput withpyproj.CRS.from_user_input.
- Args:
n: Number of points to generate; must be > 0. bbox: Bounding box sequence
(xmin, ymin, xmax, ymax). polygon: Domain polygon geometry (PolygonorMultiPolygononly). sampler: Optional callable producing candidate coordinates. random_state: Random seed for deterministic output. crs: Optional CRS user input passed throughpyproj.CRS.from_user_inputbefore assignment.max_attempts: Maximum number of evaluated candidate points.
- Returns:
A geometry-only
geopandas.GeoDataFramewithnsampled points.- Raises:
- ValueError: For invalid inputs (for example
n <= 0, invalid bbox/polygon, missing domain, invalidsampler,max_attempts <= 0, or non-positive final domain area).- RuntimeError: If sampling cannot produce
naccepted points within max_attemptsevaluated candidate points.
- ValueError: For invalid inputs (for example
- graph_to_gdf(graph: Graph, *, nodes: bool = True, edges: bool = True) tuple[GeoDataFrame | None, GeoDataFrame | None][source]#
Convert a geospatial NetworkX graph into node and edge GeoDataFrames.
This function reconstructs node and edge GeoDataFrames from a graph that stores geospatial attributes. Node geometry is taken from the stored geometry attribute when available, otherwise it falls back to a Point created from the
poscoordinates. The internalposattribute is used for reconstruction but is omitted from exported node columns. Edge geometry uses a stored shapely geometry when present; otherwise it is built as a straight LineString between endpoint positions or centroids. CRS metadata is read fromgraph.graph["crs"]and applied to outputs when present.- Args:
graph: Graph containing geospatial node/edge attributes. nodes: Whether to build the node GeoDataFrame. edges: Whether to build the edge GeoDataFrame.
- Returns:
A tuple of
(nodes_gdf, edges_gdf)withNonefor any layer not requested.- Raises:
ValueError: If both
nodesandedgesareFalse. RuntimeError: If geometry cannot be reconstructed for a node or edge.