1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#!/usr/bin/env python
###############################################################################
# $Id: vec_tr.py 7151ebd90dea6635aceeda52bcf5240112a0f065 2018-05-08 03:24:39 +1000 Ben Elliston $
#
# Project: OGR Python samples
# Purpose: Apply a transformation to all OGR geometries.
# Author: Frank Warmerdam, warmerdam@pobox.com
#
###############################################################################
# Copyright (c) 2006, Frank Warmerdam <warmerdam@pobox.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
###############################################################################
import sys
from osgeo import ogr
#############################################################################
def TransformPoint(xyz):
x = xyz[0]
y = xyz[1]
z = xyz[2]
x = x + 1000
return (x, y, z)
#############################################################################
def WalkAndTransform(geom):
if geom.GetGeometryCount() > 0:
for i in range(geom.GetGeometryCount()):
old_geom = geom.GetGeometryRef(i)
new_geom = WalkAndTransform(old_geom)
if new_geom is not old_geom:
geom.SetGeometryDirectly(new_geom)
return geom
for i in range(geom.GetPointCount()):
xyz = (geom.GetX(i), geom.GetY(i), geom.GetZ(i))
xyz = TransformPoint(xyz)
geom.SetPoint(i, xyz[0], xyz[1], xyz[2])
return geom
#############################################################################
def Usage():
print('Usage: vec_tr.py infile outfile [layer]')
print('')
sys.exit(1)
#############################################################################
# Argument processing.
infile = None
outfile = None
layer_name = None
for arg in sys.argv[1:]:
if infile is None:
infile = arg
elif outfile is None:
outfile = arg
elif layer_name is None:
layer_name = arg
else:
Usage()
if outfile is None:
Usage()
#############################################################################
# Open the datasource to operate on.
in_ds = ogr.Open(infile, update=0)
if layer_name is not None:
in_layer = in_ds.GetLayerByName(layer_name)
else:
in_layer = in_ds.GetLayer(0)
in_defn = in_layer.GetLayerDefn()
#############################################################################
# Create output file with similar information.
shp_driver = ogr.GetDriverByName('ESRI Shapefile')
shp_driver.DeleteDataSource(outfile)
shp_ds = shp_driver.CreateDataSource(outfile)
shp_layer = shp_ds.CreateLayer(in_defn.GetName(),
geom_type=in_defn.GetGeomType(),
srs=in_layer.GetSpatialRef())
in_field_count = in_defn.GetFieldCount()
for fld_index in range(in_field_count):
src_fd = in_defn.GetFieldDefn(fld_index)
fd = ogr.FieldDefn(src_fd.GetName(), src_fd.GetType())
fd.SetWidth(src_fd.GetWidth())
fd.SetPrecision(src_fd.GetPrecision())
shp_layer.CreateField(fd)
#############################################################################
# Process all features in input layer.
in_feat = in_layer.GetNextFeature()
while in_feat is not None:
geom = in_feat.GetGeometryRef().Clone()
geom = WalkAndTransform(geom)
out_feat = ogr.Feature(feature_def=shp_layer.GetLayerDefn())
out_feat.SetFrom(in_feat)
out_feat.SetGeometryDirectly(geom)
shp_layer.CreateFeature(out_feat)
out_feat.Destroy()
in_feat.Destroy()
in_feat = in_layer.GetNextFeature()
#############################################################################
# Cleanup
shp_ds.Destroy()
in_ds.Destroy()
|