GeoDataFrame
polars_st.GeoDataFrame #
GeoDataFrame(
data: FrameInitTypes | None = None,
schema: SchemaDefinition | None = None,
*,
geometry_name: str = "geometry",
geometry_format: (
Literal[
"wkb",
"wkt",
"ewkt",
"geojson",
"shapely",
"coords",
"point",
"multipoint",
"linestring",
"circularstring",
"multilinestring",
"polygon",
]
| None
) = None,
schema_overrides: SchemaDict | None = None,
strict: bool = True,
orient: Orientation | None = None,
infer_schema_length: int | None = N_INFER_DEFAULT,
nan_to_null: bool = False
)
Bases: DataFrame
Create a new GeoDataFrame.
GeoDataFrame
is used as an alias for pl.DataFrame
with type annotations added for
the st
namespace, and an overriden constructor which will
parse the column identified by geometry_name
(default "geometry"
) into a
GeoSeries
.
See pl.DataFrame
for parameters documentation.
Note
Because Polars doesn't support subclassing of their types, calling this constructor will
NOT create an instance of GeoDataFrame
, but an instance of pl.DataFrame
.
As a result, instance checks are not permitted on this class to prevent misuse:
Examples:
>>> gdf = st.GeoDataFrame({
... "geometry": [
... "POINT(0 0)",
... "POINT(1 2)",
... ]
... })
>>> gdf.schema
Schema({'geometry': Binary})
>>> gdf = st.GeoDataFrame(
... {
... "geom": [
... '{"type": "Point", "coordinates": [0, 0]}',
... '{"type": "Point", "coordinates": [1, 2]}',
... ]
... },
... geometry_name="geom",
... geometry_format="geojson",
... )
>>> gdf.schema
Schema({'geom': Binary})
polars_st.GeoDataFrameNameSpace #
polars_st.GeoDataFrameNameSpace.__geo_interface__
property
#
__geo_interface__: dict
Return a GeoJSON FeatureCollection dict
representation of the DataFrame.
Examples:
>>> gdf = st.GeoDataFrame({
... "geometry": ["POINT(0 0)", "POINT(1 2)"],
... "name": ["Alice", "Bob"]
... })
>>> interface = gdf.st.__geo_interface__
>>> pprint.pp(interface)
{'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [0.0, 0.0]},
'properties': {'name': 'Alice'}},
{'type': 'Feature',
'geometry': {'type': 'Point', 'coordinates': [1.0, 2.0]},
'properties': {'name': 'Bob'}}]}
polars_st.GeoDataFrameNameSpace.sjoin #
sjoin(
other: DataFrame,
on: str | Expr = "geometry",
how: JoinStrategy = "inner",
predicate: Literal[
"intersects_bbox",
"intersects",
"within",
"contains",
"overlaps",
"crosses",
"touches",
"covers",
"covered_by",
"contains_properly",
] = "intersects",
*,
left_on: str | Expr | None = None,
right_on: str | Expr | None = None,
suffix: str = "_right",
validate: JoinValidation = "m:m",
coalesce: bool | None = None
) -> GeoDataFrame
Perform a spatial join operation with another DataFrame.
polars_st.GeoDataFrameNameSpace.to_wkt #
to_wkt(
*geometry_columns: str,
rounding_precision: int | None = 6,
trim: bool = True,
output_dimension: Literal[2, 3, 4] = 3,
old_3d: bool = False
) -> DataFrame
Serialize the DataFrame geometry column as WKT.
polars_st.GeoDataFrameNameSpace.to_ewkt #
to_ewkt(
*geometry_columns: str,
rounding_precision: int | None = 6,
trim: bool = True,
output_dimension: Literal[2, 3, 4] = 3,
old_3d: bool = False
) -> DataFrame
Serialize the DataFrame geometry column as EWKT.
polars_st.GeoDataFrameNameSpace.to_wkb #
to_wkb(
*geometry_columns: str,
output_dimension: Literal[2, 3, 4] = 3,
byte_order: Literal[0, 1] | None = None,
include_srid: bool = False
) -> DataFrame
Serialize the DataFrame geometry column as WKB.
polars_st.GeoDataFrameNameSpace.to_geojson #
Serialize the DataFrame geometry column as GeoJSON.
polars_st.GeoDataFrameNameSpace.to_shapely #
to_shapely(*geometry_columns: str) -> DataFrame
Convert the DataFrame geometry column to a shapely representation.
polars_st.GeoDataFrameNameSpace.to_dict #
to_dict(*geometry_columns: str) -> DataFrame
Convert the DataFrame geometry column to a GeoJSON-like Python dict
representation.
polars_st.GeoDataFrameNameSpace.to_dicts #
Convert every row to a Python dict
representation of a GeoJSON Feature.
Examples:
>>> gdf = st.GeoDataFrame({
... "name": ["Alice", "Bob"],
... "location": ["POINT(0 0)", "POINT(1 2)"],
... }, geometry_name="location")
>>> dicts = gdf.st.to_dicts("location")
>>> dicts[0]
{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [0.0, 0.0]}, 'properties': {'name': 'Alice'}}
polars_st.GeoDataFrameNameSpace.to_geopandas #
to_geopandas(
*,
geometry_name: str = "geometry",
use_pyarrow_extension_array: bool = False,
**kwargs: Any
) -> GeoDataFrame
Convert this DataFrame to a geopandas GeoDataFrame.
polars_st.GeoDataFrameNameSpace.write_file #
write_file(
path: str | BytesIO,
layer: str | None = None,
driver: str | None = None,
geometry_name: str = "geometry",
encoding: str | None = None,
append: bool = False,
dataset_metadata: dict | None = None,
layer_metadata: dict | None = None,
metadata: dict | None = None,
dataset_options: dict | None = None,
layer_options: dict | None = None,
**kwargs: dict[str, Any]
) -> None
Write the GeoDataFrame to an OGR supported file format.
Parameters:
-
path
(str | BytesIO
) –path to output file on writeable file system or an io.BytesIO object to allow writing to memory NOTE: support for writing to memory is limited to specific drivers.
-
layer
(str | None
, default:None
) –layer name to create. If writing to memory and layer name is not provided, it layer name will be set to a UUID4 value.
-
driver
(str | None
, default:None
) – -
geometry_name
(str
, default:'geometry'
) –The name of the column in the input data that will be written as the geometry field.
-
encoding
(str | None
, default:None
) –Only used for the .dbf file of ESRI Shapefiles. If not specified, uses the default locale.
-
append
(bool
, default:False
) –If True, the data source specified by path already exists, and the driver supports appending to an existing data source, will cause the data to be appended to the existing records in the data source. Not supported for writing to in-memory files. NOTE: append support is limited to specific drivers and GDAL versions.
-
dataset_metadata
(dict | None
, default:None
) –Metadata to be stored at the dataset level in the output file; limited to drivers that support writing metadata, such as GPKG, and silently ignored otherwise. Keys and values must be strings.
-
layer_metadata
(dict | None
, default:None
) –Metadata to be stored at the layer level in the output file; limited to drivers that support writing metadata, such as GPKG, and silently ignored otherwise. Keys and values must be strings.
-
metadata
(dict | None
, default:None
) –alias of layer_metadata
-
dataset_options
(dict | None
, default:None
) –Dataset creation options (format specific) passed to OGR. Specify as a key-value dictionary.
-
layer_options
(dict | None
, default:None
) –Layer creation options (format specific) passed to OGR. Specify as a key-value dictionary.
-
**kwargs
(dict[str, Any]
, default:{}
) –Additional driver-specific dataset or layer creation options passed to OGR. pyogrio will attempt to automatically pass those keywords either as dataset or as layer creation option based on the known options for the specific driver. Alternatively, you can use the explicit
dataset_options
orlayer_options
keywords to manually do this (for example if an option exists as both dataset and layer option).
polars_st.GeoDataFrameNameSpace.write_geojson #
Serialize to GeoJSON FeatureCollection representation.
The result will be invalid if the geometry column contains different geometry types.
Examples:
>>> gdf = st.GeoDataFrame({
... "geometry": ["POINT(0 0)", "POINT(1 2)"],
... "name": ["Alice", "Bob"]
... })
>>> geojson = gdf.st.write_geojson()
>>> print(geojson)
{"type":"FeatureCollection","features":[{"properties":{"name":"Alice"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},{"properties":{"name":"Bob"},"geometry":{"type":"Point","coordinates":[1.0,2.0]}}]}
polars_st.GeoDataFrameNameSpace.write_ndgeojson #
Serialize to newline-delimited GeoJSON representation.
The result will be invalid if the geometry column contains different geometry types.
Examples:
>>> gdf = st.GeoDataFrame({
... "geometry": ["POINT(0 0)", "POINT(1 2)"],
... "name": ["Alice", "Bob"]
... })
>>> ndgeojson = gdf.st.write_ndgeojson()
>>> print(ndgeojson)
{"properties":{"name":"Alice"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}}
{"properties":{"name":"Bob"},"geometry":{"type":"Point","coordinates":[1.0,2.0]}}
polars_st.GeoDataFrameNameSpace.plot #
plot(
geometry_name: str = "geometry", **kwargs: Unpack[MarkConfigKwds]
) -> Chart
Draw map plot.
Polars does not implement plotting logic itself but instead defers to
Altair
.
df.st.plot(**kwargs)
is shorthand for
alt.Chart({"values": df.st.to_dicts()}).mark_geoshape(**kwargs).interactive()
. Please read
Altair GeoShape documentation
for available options.
Please note that the dataframe will be converted to a GeoJSON FeatureCollection, so
columns will need to be prefixed with properties.
for access in Altair functions.
Examples:
>>> url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
>>> plot = (
... st.read_file(url)
... .with_columns(st.simplify(tolerance=1))
... .st.plot()
... .encode(color="properties.CONTINENT:N")
... .configure_legend(title=None)
... .properties(height=150)
... )
>>> import altair as alt
>>> df = st.GeoDataFrame({
... "color": ["red","yellow", "blue"],
... "geometry": [
... "POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))",
... "POLYGON((0 0, 1 2, 2 0, 0 0))",
... "POINT(2 1)"
... ]})
>>> plot = (
... df.st.plot(blend="difference")
... .encode(fill=alt.Color("properties.color:N", scale=None))
... .project("identity", reflectY=True, pointRadius=100)
... .properties(height=200)
... )