You cannot Directly transform WKT Strings with orgr2ogr
.
But you can read and write WKT Geometries as part of the CSV
format, see http://www.gdal.org/drv_csv.html and https://trac.osgeo.org/gdal/ticket/3145.
So you could write a wrapper function to create and write temporary CSV files from your WKT and parse the result.
In Bash:Bash:
function wkt_transform {
test $# -eq 3 || { >&2 echo -e "Usage:\n wkt_transform WKT_STRING S_SRS T_SRS"; return 1; }
tmpcsv=$(mktemp -u).csv
echo -e "id,WKT\n1,\"${1}\"" > $tmpcsv
ogr2ogr -f CSV -s_srs $2 -t_srs $3 /vsistdout/ $tmpcsv \
-oo GEOM_POSSIBLE_NAMES=WKT -oo KEEP_GEOM_COLUMNS=false -lco GEOMETRY=AS_WKT \
| grep --color=never -Po '(?<=^")[^"]*'
rm $tmpcsv
}
Usage:
wkt_transform WKT_STRING S_SRS T_SRS
Example:
wkt_transform "POLYGON ((413690.7172644 5316365.30576906,413705.941127458 5316362.57674411,413708.340269542 5316375.86138841,413693.256350828 5316378.66038415,413690.7172644 5316365.30576906))" EPSG:31467 EPSG:25832
Result:
POLYGON ((-2575200.84161077 5311948.51145795,-2575186.10407411 5311945.92874797,-2575183.55059298 5311959.14935342,-2575198.15104713 5311961.80277235,-2575200.84161077 5311948.51145795))
Python alternative:
import sys, re
from osgeo import ogr
from osgeo import osr
wkt = sys.argv[1]
s_srs = int(re.sub("[^0-9]", "", sys.argv[2]))
t_srs = int(re.sub("[^0-9]", "", sys.argv[3]))
source = osr.SpatialReference()
source.ImportFromEPSG(s_srs)
target = osr.SpatialReference()
target.ImportFromEPSG(t_srs)
transform = osr.CoordinateTransformation(source, target)
geom = ogr.CreateGeometryFromWkt(wkt)
geom.Transform(transform)
print geom.ExportToWkt()
Example:
transform.py "POLYGON ((413690.7172644 5316365.30576906,413705.941127458 5316362.57674411,413708.340269542 5316375.86138841,413693.256350828 5316378.66038415,413690.7172644 5316365.30576906))" 31467 25832