Skip to content

Commit

Permalink
Merge pull request #41 from srimaln91/dev
Browse files Browse the repository at this point in the history
Minor improvements in error handling
  • Loading branch information
srimaln91 authored Jul 26, 2019
2 parents ec2ccd1 + c99da08 commit e3c6b49
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
2 changes: 2 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[url "git@git.mytaxi.lk:"]
insteadOf = https://git.mytaxi.lk/
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
```
8 changes: 4 additions & 4 deletions geom/geom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

func TestLwGeomBufferWithParams(t *testing.T) {
lwgeom := FromGeoJSON(JSONLinestring)
lwgeom, _ := FromGeoJSON(JSONLinestring)
lwgeom.SetSRID(4326)
defer lwgeom.Free()

Expand All @@ -27,15 +27,15 @@ 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()")
}
}

func TestLwGeomBuffer(t *testing.T) {
lwgeom := FromGeoJSON(JSONLinestring)
lwgeom, _ := FromGeoJSON(JSONLinestring)
lwgeom.SetSRID(4326)

defer lwgeom.Free()
Expand All @@ -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()")
Expand Down
18 changes: 13 additions & 5 deletions geom/lwgeom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 23 additions & 21 deletions geom/lwgeom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestGeomFromGeoJson(t *testing.T) {

geom := FromGeoJSON(JSONLinestring)
geom, _ := FromGeoJSON(JSONLinestring)

if geom == nil {
t.Error("Error: GeomFromGeoJson()")
Expand All @@ -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()")
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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,
Expand All @@ -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()")
Expand All @@ -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")

Expand All @@ -159,36 +159,38 @@ 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()")
}
}

func TestLineLocatePoint(t *testing.T) {
linestring := FromGeoJSON(JSONLinestring)
linestring, _ := FromGeoJSON(JSONLinestring)
defer linestring.Free()

point := FromGeoJSON(`{
point, _ := FromGeoJSON(`{
"type": "Point",
"coordinates": [
79.91254448890686,
Expand All @@ -208,7 +210,7 @@ func TestLineLocatePoint(t *testing.T) {

func TestGEOSUnion(t *testing.T) {

lwgeom1 := FromGeoJSON(`{
lwgeom1, _ := FromGeoJSON(`{
"type": "Polygon",
"coordinates": [
[
Expand Down Expand Up @@ -237,7 +239,7 @@ func TestGEOSUnion(t *testing.T) {
}
`)

lwgeom2 := FromGeoJSON(`{
lwgeom2, _ := FromGeoJSON(`{
"type": "Polygon",
"coordinates": [
[
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module git.mytaxi.lk/pickme/geo/libs/go-geom

go 1.12

0 comments on commit e3c6b49

Please sign in to comment.