本文整理汇总了Python中osgeo.ogr.Geometry方法的典型用法代码示例。如果您正苦于以下问题:Python ogr.Geometry方法的具体用法?Python ogr.Geometry怎么用?Python ogr.Geometry使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类osgeo.ogr
的用法示例。
在下文中一共展示了ogr.Geometry方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: multiple_union
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def multiple_union(polygons):
"""
Takes a list of polygons and returns a polygon of the union of their perimeter
Parameters
----------
polygons
A list of ogr.Geometry objects, each containing a single polygon.
Returns
-------
An ogr.Geometry object containing a single polygon
"""
# Note; I can see this maybe failing(or at least returning a multipolygon)
# if two consecutive polygons do not overlap at all. Keep eye on.
running_union = polygons[0]
for polygon in polygons[1:]:
running_union = running_union.Union(polygon)
return running_union.Simplify(0)
示例2: multiple_intersection
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def multiple_intersection(polygons):
"""
Takes a list of polygons and returns a geometry representing the intersection of all of them
Parameters
----------
polygons
A list of ogr.Geometry objects, each containing a single polygon.
Returns
-------
An ogr.Geometry object containing a single polygon
"""
running_intersection = polygons[0]
for polygon in polygons[1:]:
running_intersection = running_intersection.Intersection(polygon)
return running_intersection.Simplify(0)
示例3: get_aoi_intersection
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_aoi_intersection(raster, aoi):
"""
Returns a wkbPolygon geometry with the intersection of a raster and a shpefile containing an area of interest
Parameters
----------
raster
A raster containing image data
aoi
A shapefile with a single layer and feature
Returns
-------
a ogr.Geometry object containing a single polygon with the area of intersection
"""
raster_shape = get_raster_bounds(raster)
aoi.GetLayer(0).ResetReading() # Just in case the aoi has been accessed by something else
aoi_feature = aoi.GetLayer(0).GetFeature(0)
aoi_geometry = aoi_feature.GetGeometryRef()
return aoi_geometry.Intersection(raster_shape)
示例4: get_raster_intersection
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_raster_intersection(raster1, raster2):
"""
Returns a wkbPolygon geometry with the intersection of two raster bounding boxes.
Parameters
----------
raster1, raster2
A gdal.Image() object
Returns
-------
a ogr.Geometry object containing a single polygon
"""
bounds_1 = get_raster_bounds(raster1)
bounds_2 = get_raster_bounds(raster2)
return bounds_1.Intersection(bounds_2)
示例5: get_aoi_bounds
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_aoi_bounds(aoi):
"""
Returns a wkbPolygon geometry with the bounding rectangle of a single-polygon shapefile
Parameters
----------
aoi
An ogr.Dataset object containing a single layer.
Returns
-------
"""
aoi_bounds = ogr.Geometry(ogr.wkbLinearRing)
(x_min, x_max, y_min, y_max) = aoi.GetLayer(0).GetExtent()
aoi_bounds.AddPoint(x_min, y_min)
aoi_bounds.AddPoint(x_max, y_min)
aoi_bounds.AddPoint(x_max, y_max)
aoi_bounds.AddPoint(x_min, y_max)
aoi_bounds.AddPoint(x_min, y_min)
bounds_poly = ogr.Geometry(ogr.wkbPolygon)
bounds_poly.AddGeometry(aoi_bounds)
return bounds_poly
示例6: get_poly_size
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_poly_size(poly):
"""
Returns the width and height of a bounding box of a polygon
Parameters
----------
poly
A ogr.Geometry object containing the polygon.
Returns
-------
A tuple of (width, height).
"""
boundary = poly.Boundary()
x_min, y_min, not_needed = boundary.GetPoint(0)
x_max, y_max, not_needed = boundary.GetPoint(2)
out = (x_max - x_min, y_max-y_min)
return out
示例7: get_poly_bounding_rect
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_poly_bounding_rect(poly):
"""
Returns a polygon of the bounding rectangle of an input polygon.
Parameters
----------
poly
An ogr.Geometry object containing a polygon
Returns
-------
An ogr.Geometry object with a four-point polygon representing the bounding rectangle.
"""
aoi_bounds = ogr.Geometry(ogr.wkbLinearRing)
x_min, x_max, y_min, y_max = poly.GetEnvelope()
aoi_bounds.AddPoint(x_min, y_min)
aoi_bounds.AddPoint(x_max, y_min)
aoi_bounds.AddPoint(x_max, y_max)
aoi_bounds.AddPoint(x_min, y_max)
aoi_bounds.AddPoint(x_min, y_min)
bounds_poly = ogr.Geometry(ogr.wkbPolygon)
bounds_poly.AddGeometry(aoi_bounds)
return bounds_poly
示例8: plot
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def plot(self, geom_or_lyr, symbol='', name='', **kwargs):
"""Plot a geometry or layer.
geom_or_lyr - geometry, layer, or filename
symbol - optional pyplot symbol to draw the geometries with
name - optional name to assign to layer so can access it later
kwargs - optional pyplot drawing parameters
"""
if type(geom_or_lyr) is str:
lyr, ds = pb._get_layer(geom_or_lyr)
self.plot_layer(lyr, symbol, name, **kwargs)
elif type(geom_or_lyr) is ogr.Geometry:
self.plot_geom(geom_or_lyr, symbol, name, **kwargs)
elif type(geom_or_lyr) is ogr.Layer:
self.plot_layer(geom_or_lyr, symbol, name, **kwargs)
else:
raise RuntimeError('{} is not supported.'.format(type(geom_or_lyr)))
示例9: ogr_add_geometry
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def ogr_add_geometry(layer, geom, attrs):
""" Copies single OGR.Geometry object to an OGR.Layer object.
Given OGR.Geometry is copied to new OGR.Feature and
written to given OGR.Layer by given index. Attributes are attached.
Parameters
----------
layer : OGR.Layer
object
geom : OGR.Geometry
object
attrs : list
attributes referring to layer fields
"""
defn = layer.GetLayerDefn()
feat = ogr.Feature(defn)
for i, item in enumerate(attrs):
feat.SetField(i, item)
feat.SetGeometry(geom)
layer.CreateFeature(feat)
示例10: ogr_to_numpy
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def ogr_to_numpy(ogrobj):
"""Backconvert a gdal/ogr geometry to a numpy vertex array.
Using JSON as a vehicle to efficiently deal with numpy arrays.
Parameters
----------
ogrobj : ogr.Geometry
object
Returns
-------
out : :class:`numpy:numpy.ndarray`
a nested ndarray of vertices of shape (num vertices, 2)
"""
jsonobj = eval(ogrobj.ExportToJson())
return np.squeeze(jsonobj["coordinates"])
示例11: get_centroid
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_centroid(polyg):
"""Return centroid of a polygon
Parameters
----------
polyg : :class:`numpy:numpy.ndarray`
of shape (num vertices, 2) or ogr.Geometry object
Returns
-------
out : x and y coordinate of the centroid
"""
if not type(polyg) == ogr.Geometry:
polyg = numpy_to_ogr(polyg, "Polygon")
return polyg.Centroid().GetPoint()[0:2]
示例12: main
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def main():
args = parse_arguments()
src_ds = gdal.Open(args.src_file[0])
driver = gdal.GetDriverByName("GTiff")
dst_ds = driver.CreateCopy(args.out_file[0], src_ds, 0)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(float(args.position[0]), float(args.position[1]))
point.Transform(transform)
gt = [point.GetX(), float(args.pixelsize[0]), 0,
point.GetY(), 0, -float(args.pixelsize[1])]
dst_ds.SetGeoTransform(gt)
to_sys = osr.SpatialReference()
to_sys.ImportFromEPSG(to_srs)
dest_wkt = to_sys.ExportToWkt()
dst_ds.SetProjection(dest_wkt)
示例13: raster_to_spatial
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def raster_to_spatial(self, xy):
"""Convert xy raster coordinates to spatial coordinates
Parameters
----------
xy: sequence of numbers of shape (..., 2)
Raster coordinages
Returns
-------
out_xy: np.ndarray
Spatial coordinates
with shape = np.asarray(xy).shape
with dtype = dtype
"""
# Check xy parameter
xy = np.asarray(xy)
if xy.shape[-1] != 2:
raise ValueError('An array of shape (..., 2) was expected') # pragma: no cover
workshape = int(xy.size / 2), 2
xy2 = np.empty(workshape, 'float64')
xy2[:, :] = xy.reshape(workshape)
aff = self._aff
xy2[:, 0], xy2[:, 1] = (
xy2[:, 0] * aff.a + xy2[:, 1] * aff.b + aff.c,
xy2[:, 0] * aff.d + xy2[:, 1] * aff.e + aff.f,
)
return xy2.reshape(xy.shape)
# Geometry / Raster conversions ************************************************************* **
示例14: get_combined_polygon
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def get_combined_polygon(rasters, geometry_mode ="intersect"):
"""
Returns a polygon containing the combined boundary of each raster in rasters.
Parameters
----------
rasters
A list of raster objects opened with gdal.Open()
geometry_mode
If 'intersect', returns the boundary of the area that all rasters cover.
If 'union', returns the boundary of the area that any raster covers.
Returns
-------
ogr.Geometry() containing a polygon.
"""
raster_bounds = []
for in_raster in rasters:
raster_bounds.append(get_raster_bounds(in_raster))
# Calculate overall bounding box based on either union or intersection of rasters
if geometry_mode == "intersect":
combined_polygons = multiple_intersection(raster_bounds)
elif geometry_mode == "union":
combined_polygons = multiple_union(raster_bounds)
else:
raise Exception("Invalid geometry mode")
return combined_polygons
示例15: point_to_pixel_coordinates
# 需要导入模块: from osgeo import ogr [as 别名]
# 或者: from osgeo.ogr import Geometry [as 别名]
def point_to_pixel_coordinates(raster, point, oob_fail=False):
"""
Returns a tuple (x_pixel, y_pixel) in a georaster raster corresponding to the geographic point in a projection.
Assumes raster is north-up non rotated.
Parameters
----------
raster
A gdal raster object
point
One of:
A well-known text string of a single point
An iterable of the form (x,y)
An ogr.Geometry object containing a single point
Returns
-------
A tuple of (x_pixel, y_pixel), containing the indicies of the point in the raster.
Notes
-----
The equation is a rearrangement of the section on affinine geotransform in http://www.gdal.org/gdal_datamodel.html
"""
if isinstance(point, str):
point = ogr.CreateGeometryFromWkt(point)
x_geo = point.GetX()
y_geo = point.GetY()
if isinstance(point, list) or isinstance(point, tuple): # There is a more pythonic way to do this
x_geo = point[0]
y_geo = point[1]
if isinstance(point, ogr.Geometry):
x_geo = point.GetX()
y_geo = point.GetY()
gt = raster.GetGeoTransform()
x_pixel = int(np.floor((x_geo - floor_to_resolution(gt[0], gt[1]))/gt[1]))
y_pixel = int(np.floor((y_geo - floor_to_resolution(gt[3], gt[5]*-1))/gt[5])) # y resolution is -ve
return x_pixel, y_pixel