-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix comparisons for null jsons and geo strings
- Loading branch information
1 parent
2cfcd10
commit fad17cd
Showing
5 changed files
with
76 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package geo | ||
|
||
import ( | ||
"encoding/hex" | ||
"errors" | ||
"fmt" | ||
"log/slog" | ||
|
||
geom "github.com/twpayne/go-geos" | ||
) | ||
|
||
// returns the WKT representation of the geometry object if it is valid | ||
func GeoValidate(hexWkb string) (string, error) { | ||
// Decode the WKB hex string into binary | ||
wkb, hexErr := hex.DecodeString(hexWkb) | ||
if hexErr != nil { | ||
slog.Warn(fmt.Sprintf("Ignoring invalid WKB: %s", hexWkb)) | ||
return "", hexErr | ||
} | ||
|
||
// UnmarshalWKB performs geometry validation along with WKB parsing | ||
geometryObject, geoErr := geom.NewGeomFromWKB(wkb) | ||
if geoErr != nil { | ||
return "", geoErr | ||
} | ||
|
||
invalidReason := geometryObject.IsValidReason() | ||
if invalidReason != "Valid Geometry" { | ||
slog.Warn(fmt.Sprintf("Ignoring invalid geometry shape %s: %s", hexWkb, invalidReason)) | ||
return "", errors.New(invalidReason) | ||
} | ||
|
||
wkt := geometryObject.ToWKT() | ||
return wkt, nil | ||
} | ||
|
||
// compares WKTs | ||
func GeoCompare(wkt1, wkt2 string) bool { | ||
geom1, geoErr := geom.NewGeomFromWKT(wkt1) | ||
if geoErr != nil { | ||
return false | ||
} | ||
|
||
geom2, geoErr := geom.NewGeomFromWKT(wkt2) | ||
if geoErr != nil { | ||
return false | ||
} | ||
|
||
// Compare the two geometries | ||
compareResult := geom1.Equals(geom2) | ||
return compareResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters