Question

I'm trying to validate data passing between client/server for a location-based application and I can't find any way to validate WKT strings in PHP. I'd prefer not to implement it myself, not because I'm lazy or incompetent but because I'm afraid of getting it wrong because I've never dealt with WKT before. Is there any way for me to validate or must I write my own validator?

Was it helpful?

Solution

I found this class gisconverter.php which can convert WKT to various formats, or other formats to WKT. It appears it will throw an exception if the WKT is not well formed also.

Example:

$decoder = new gisconverter\WKT(); # create a WKT decoder in gisconverter namespace
try {
    $geometry = $decoder->geomFromText('MULTIPOLYGON(((10 10,10 20,20 20,20 15,10 10)))'); # create a geometry from a given string input

    print $geometry->toGeoJSON(); # output geometry in GeoJSON format
} catch (InvalidText $itex) {
    echo "WKT was not well formed!";
} catch (Exception $ex) {
    echo "General exception.";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top