diff --git a/.gitconfig b/.gitconfig new file mode 100644 index 0000000..f6e678f --- /dev/null +++ b/.gitconfig @@ -0,0 +1,2 @@ +[url "git@git.mytaxi.lk:"] + insteadOf = https://git.mytaxi.lk/ diff --git a/README.md b/README.md index 166ac8e..9884ce8 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,13 @@ func main() { ] }` - lwgeom := geom.FromGeoJSON(jsonLineString) - lwgeom.SetSRID(4326) + lwgeom, err := geom.FromGeoJSON(jsonLineString) + + if err != nil { + panic(err) + } + lwgeom.SetSRID(4326) defer lwgeom.Free() lwgeom.LineSubstring(0.5, 0.9) @@ -122,8 +126,10 @@ func main() { // Reset SRS to EPSG:4326 lwgeom.Project(toSRS, fromSRS) - bufJSON := lwgeom.ToGeoJSON(4, 0) - + bufJSON, err := lwgeom.ToGeoJSON(4, 0) + if err != nil { + panic(err) + } fmt.Println(bufJSON) } ``` diff --git a/geom/geom_test.go b/geom/geom_test.go index 373bf5b..ee29a82 100644 --- a/geom/geom_test.go +++ b/geom/geom_test.go @@ -5,7 +5,7 @@ import ( ) func TestLwGeomBufferWithParams(t *testing.T) { - lwgeom := FromGeoJSON(JSONLinestring) + lwgeom, _ := FromGeoJSON(JSONLinestring) lwgeom.SetSRID(4326) defer lwgeom.Free() @@ -27,7 +27,7 @@ func TestLwGeomBufferWithParams(t *testing.T) { lwgeom.Project(toSRS, fromSRS) - bufJSON := lwgeom.ToGeoJSON(4, 0) + bufJSON, _ := lwgeom.ToGeoJSON(4, 0) if bufJSON == "" { t.Error("Error: BufferWithParams()") @@ -35,7 +35,7 @@ func TestLwGeomBufferWithParams(t *testing.T) { } func TestLwGeomBuffer(t *testing.T) { - lwgeom := FromGeoJSON(JSONLinestring) + lwgeom, _ := FromGeoJSON(JSONLinestring) lwgeom.SetSRID(4326) defer lwgeom.Free() @@ -49,7 +49,7 @@ func TestLwGeomBuffer(t *testing.T) { lwgeom.Buffer(200) lwgeom.Project(toSRS, fromSRS) - bufJSON := lwgeom.ToGeoJSON(4, 0) + bufJSON, _ := lwgeom.ToGeoJSON(4, 0) if bufJSON == "" { t.Error("Error: Buffer()") diff --git a/geom/lwgeom.go b/geom/lwgeom.go index 1704886..eb88896 100644 --- a/geom/lwgeom.go +++ b/geom/lwgeom.go @@ -43,15 +43,19 @@ func GEOSVersion() string { } // FromGeoJSON creates lwgeom from GeoJson -func FromGeoJSON(geojson string) *Geom { +func FromGeoJSON(geojson string) (*Geom, error) { geojsonCstring := C.CString(geojson) - lwgeom := C.lwgeom_from_geojson(geojsonCstring, &C.cnull) defer C.lwfree(unsafe.Pointer(geojsonCstring)) + lwgeom := C.lwgeom_from_geojson(geojsonCstring, &C.cnull) + if lwgeom == nil { + return nil, errors.New("Lwgeom exception on lwgeom_from_geojson") + } + return &Geom{ LwGeom: lwgeom, - } + }, nil } // LwGeomFromGEOS convert GEOS geometry to lwgeom @@ -70,11 +74,15 @@ func (lwg *Geom) Free() { } // ToGeoJSON generates geojson from lwgeom -func (lwg *Geom) ToGeoJSON(precisoin int, hasBbox int) string { +func (lwg *Geom) ToGeoJSON(precisoin int, hasBbox int) (string, error) { geojson := C.lwgeom_to_geojson(lwg.LwGeom, C.cnull, C.int(precisoin), C.int(hasBbox)) + if geojson == nil { + return "", errors.New("Lwgeom exception on lwgeom_to_geojson") + } defer C.lwfree(unsafe.Pointer(geojson)) - return C.GoString(geojson) + + return C.GoString(geojson), nil } // LineSubstring returns a part of the linestring diff --git a/geom/lwgeom_test.go b/geom/lwgeom_test.go index adb5074..6503ec2 100644 --- a/geom/lwgeom_test.go +++ b/geom/lwgeom_test.go @@ -6,7 +6,7 @@ import ( func TestGeomFromGeoJson(t *testing.T) { - geom := FromGeoJSON(JSONLinestring) + geom, _ := FromGeoJSON(JSONLinestring) if geom == nil { t.Error("Error: GeomFromGeoJson()") @@ -17,8 +17,8 @@ func TestGeomFromGeoJson(t *testing.T) { func TestToGeoJson(t *testing.T) { - geom := FromGeoJSON(JSONLinestring) - jsonString := geom.ToGeoJSON(4, 0) + geom, _ := FromGeoJSON(JSONLinestring) + jsonString, _ := geom.ToGeoJSON(4, 0) if jsonString == "" { t.Error("Error: LwGeomToGeoJson()") @@ -31,9 +31,9 @@ func TestLineSubstring(t *testing.T) { expectedJSON := `{"type":"LineString","coordinates":[[79.9066,6.8597],[79.9073,6.859],[79.9076,6.8588],[79.9078,6.8585],[79.908,6.8582],[79.9083,6.858],[79.9084,6.8579],[79.9085,6.8578],[79.9088,6.8575],[79.9089,6.8573],[79.9089,6.8573]]}` - geom := FromGeoJSON(JSONLinestring) + geom, _ := FromGeoJSON(JSONLinestring) geom.LineSubstring(0.5, 0.52) - resultJSON := geom.ToGeoJSON(4, 0) + resultJSON, _ := geom.ToGeoJSON(4, 0) if resultJSON != expectedJSON { t.Error("Error: LineSubstring()", resultJSON) @@ -44,7 +44,7 @@ func TestLineSubstring(t *testing.T) { func TestToGEOS(t *testing.T) { - geom := FromGeoJSON(JSONLinestring) + geom, _ := FromGeoJSON(JSONLinestring) geos := geom.ToGEOS() coords, _ := geos.GetNumCoordinates() @@ -74,7 +74,7 @@ func TestLwGeomFromGEOS(t *testing.T) { func TestProject(t *testing.T) { - geom := FromGeoJSON(JSONLinestring) + geom, _ := FromGeoJSON(JSONLinestring) geom.SetSRID(4326) fromSRS := SRS["EPSG:4326"] @@ -117,8 +117,8 @@ func TestGEOSVersion(t *testing.T) { } func TestClosestPoint(t *testing.T) { - geom1 := FromGeoJSON(JSONLinestring) - geom2 := FromGeoJSON(`{ + geom1, _ := FromGeoJSON(JSONLinestring) + geom2, _ := FromGeoJSON(`{ "type": "Point", "coordinates": [ 79.92603331804276, @@ -132,7 +132,7 @@ func TestClosestPoint(t *testing.T) { t.Error("Error: ClosestPoint()") } - geoJSON := closestPoint.ToGeoJSON(6, 0) + geoJSON, _ := closestPoint.ToGeoJSON(6, 0) if geoJSON != `{"type":"Point","coordinates":[79.925546,6.848402]}` { t.Error("Error: ClosestPoint()") @@ -145,8 +145,8 @@ func TestClosestPoint(t *testing.T) { } func TestSplit(t *testing.T) { - geom1 := FromGeoJSON(GetFileContents("../testdata/split/source.json")) - blade := FromGeoJSON(GetFileContents("../testdata/split/blade.json")) + geom1, _ := FromGeoJSON(GetFileContents("../testdata/split/source.json")) + blade, _ := FromGeoJSON(GetFileContents("../testdata/split/blade.json")) // expectedResult := GetFileContents("../testdata/split/result.json") @@ -159,25 +159,27 @@ func TestSplit(t *testing.T) { } func TestSubGeom(t *testing.T) { - geom1 := FromGeoJSON(GetFileContents("../testdata/split/source.json")) + geom1, _ := FromGeoJSON(GetFileContents("../testdata/split/source.json")) geom1.SetSRID(4326) - blade := FromGeoJSON(GetFileContents("../testdata/split/blade.json")) + blade, _ := FromGeoJSON(GetFileContents("../testdata/split/blade.json")) blade.SetSRID(4326) - expectedResult := FromGeoJSON(GetFileContents("../testdata/split/result.json")) + expectedResult, _ := FromGeoJSON(GetFileContents("../testdata/split/result.json")) collection, _ := geom1.Split(blade) selectedGeom, _ := collection.GetSubGeom(0) - if selectedGeom.ToGeoJSON(4, 0) != expectedResult.ToGeoJSON(4, 0) { + expResultJSON, _ := expectedResult.ToGeoJSON(4, 0) + selectedGeomJSON, _ := selectedGeom.ToGeoJSON(4, 0) + if selectedGeomJSON != expResultJSON { t.Error("Error: SplitAndSubGeom()") } } func TestLwGeomEquals(t *testing.T) { - geom := FromGeoJSON(JSONLinestring) + geom, _ := FromGeoJSON(JSONLinestring) if !geom.Equals(geom) { t.Error("Error: Equals()") @@ -185,10 +187,10 @@ func TestLwGeomEquals(t *testing.T) { } func TestLineLocatePoint(t *testing.T) { - linestring := FromGeoJSON(JSONLinestring) + linestring, _ := FromGeoJSON(JSONLinestring) defer linestring.Free() - point := FromGeoJSON(`{ + point, _ := FromGeoJSON(`{ "type": "Point", "coordinates": [ 79.91254448890686, @@ -208,7 +210,7 @@ func TestLineLocatePoint(t *testing.T) { func TestGEOSUnion(t *testing.T) { - lwgeom1 := FromGeoJSON(`{ + lwgeom1, _ := FromGeoJSON(`{ "type": "Polygon", "coordinates": [ [ @@ -237,7 +239,7 @@ func TestGEOSUnion(t *testing.T) { } `) - lwgeom2 := FromGeoJSON(`{ + lwgeom2, _ := FromGeoJSON(`{ "type": "Polygon", "coordinates": [ [ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bfb3b55 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.mytaxi.lk/pickme/geo/libs/go-geom + +go 1.12