Get bounding box and/or center point as lat/lon from VectorLayer

Hi there,

I’m opening a variety of files (ESRI Shapefile, GeoJSON, etc.) using FileDriver.OpenLayer, and I’d like to find the geolocation of the shape represented in the layer.

Assuming there’s a spatial reference system assigned in the layer, is there an easy way to calculate a lat/lon bounding box for the shape, and/or the center point as lat/lon?

I read through the documentation and couldn’t see a way to do this directly.

Thanks,
Kirk

@kirkmarple,

We will evaluate your requirements and get back to you soon.

1 Like

Hi, @kirkmarple

Please consider the below code

            // Get center and extent in current layer crs
            var layerExtent = layer.GetExtent().ToPolygon();
            var layerCenter = layer.GetExtent().Center;

            // Create a transformation from UTM to WGS84 (longitude, latitude)
            var targetSrs = SpatialReferenceSystem.Wgs84;
            var srsTransformation = layer.SpatialReferenceSystem.CreateTransformationTo(targetSrs);

            // Convert in lat long
            var layerExtentAsLatLong = srsTransformation.Transform(layerExtent);
            var rect = layerExtentAsLatLong.GetExtent();
            var layerCenterAsLatLong = srsTransformation.Transform(layerCenter);

Thanks

Great, thanks! I’ll try that out.