1. 主页
  2. 文档
  3. GeoPandas 0.12.2中文文档
  4. Advanced Guide
  5. Re-projecting using GDAL with Rasterio and Fiona
  6. Fiona Example

Fiona Example

from functools import partial

import fiona
import geopandas
from fiona.transform import transform_geom
from packaging import version
from pyproj import CRS
from pyproj.enums import WktVersion
from shapely.geometry import mapping, shape


# set up Fiona transformer
def crs_to_fiona(proj_crs):
    proj_crs = CRS.from_user_input(proj_crs)
    if version.parse(fiona.__gdal_version__) < version.parse("3.0.0"):
        fio_crs = proj_crs.to_wkt(WktVersion.WKT1_GDAL)
    else:
        # GDAL 3+ can use WKT2
        fio_crs = proj_crs.to_wkt()
    return fio_crs

def base_transformer(geom, src_crs, dst_crs):
    return shape(
        transform_geom(
            src_crs=crs_to_fiona(src_crs),
            dst_crs=crs_to_fiona(dst_crs),
            geom=mapping(geom),
            antimeridian_cutting=True,
        )
    )

# load example data
world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

destination_crs = "EPSG:3395"
forward_transformer = partial(base_transformer, src_crs=world.crs, dst_crs=destination_crs)

# Reproject to Mercator (after dropping Antartica)
world = world[(world.name != "Antarctica") & (world.name != "Fr. S. Antarctic Lands")]
with fiona.Env(OGR_ENABLE_PARTIAL_REPROJECTION="YES"):
    mercator_world = world.set_geometry(world.geometry.apply(forward_transformer), crs=destination_crs)
标签 , ,
这篇文章对您有用吗?

我们要如何帮助您?

欢迎留下您的宝贵建议

Please enter your comment!
Please enter your name here