2

I am looking for a way to transform WKT-strings from EPSG:31467 to EPSG:25832 using a ntv2-grid (and without using a gui).

Example:

POLYGON((3413732.42 5318051.77,3413747.65 5318049.04,3413750.05 5318062.33,3413734.96 5318065.13,3413732.42 5318051.77))

For shapefiles I use ogr2ogr but have no clue how to transform WKT strings.

1 Answer 1

2

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.


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
1
  • works great and the performance is good even with a NTv2-grid of nearly 400MB size
    – Thomas B
    Commented Apr 6, 2018 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.