Skip to main content
added 762 characters in body
Source Link
pLumo
  • 6.6k
  • 21
  • 42

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

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:

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))

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
added 1 character in body
Source Link
pLumo
  • 6.6k
  • 21
  • 42

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:

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 '(?<=^")[^"]*' | xargs echo
    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))

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:

function wkt_transform {
    test $# -eq 3 || { >&2 echo -e "Usage: 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 -Po '(?<=^")[^"]*' | xargs echo
    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))

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:

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))

added 220 characters in body
Source Link
pLumo
  • 6.6k
  • 21
  • 42

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:

function wkt_transform {
    test $# -eq 3 || { >&2 echo -e "Usage: 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 -Po '(?<=^")[^"]*' | xargs echo
    rm -f $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))

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:

function wkt_transform {
    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 -Po '(?<=^")[^"]*' | xargs echo
    rm -f $tmpcsv
}

Usage:

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))

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:

function wkt_transform {
    test $# -eq 3 || { >&2 echo -e "Usage: 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 -Po '(?<=^")[^"]*' | xargs echo
    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))

Source Link
pLumo
  • 6.6k
  • 21
  • 42
Loading