twpayne/go-geos

Invalid memory address on CoordSeq()

pauletienney opened this issue · 3 comments

I always have the same error when trying to execute CoordSeq() on a single geom. Here is the error :

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102861c84]

goroutine 1 [running]:
github.com/twpayne/go-geos.(*Geom).CoordSeq(0x14000024100)
	/Users/paul/go/pkg/mod/github.com/twpayne/go-geos@v0.5.0/geom.go:56 +0xe4
sherlock/cmd.glob..func4(0x102d0bc40?, {0x1029afab9?, 0x0?, 0x0?})
	/Users/paul/projects/sherlock-import/cmd/sandbox.go:27 +0x160
github.com/spf13/cobra.(*Command).execute(0x102d0bc40, {0x102d41530, 0x0, 0x0})
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 +0x5b0
github.com/spf13/cobra.(*Command).ExecuteC(0x102d0b960)
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968
sherlock/cmd.Execute()
	/Users/paul/projects/sherlock-import/cmd/root.go:20 +0x28
main.main()
	/Users/paul/projects/sherlock-import/main.go:6 +0x1c

Here is the code I execute, the structs I used and the output of the terminal:

Executed code

plots := models.LoadPlots("33118")
fmt.Println("loaded ", len(plots.Plots), " plots")

geom := plots.Plots[0].Geom.Geom

fmt.Println(geom.IsValid())
fmt.Println(geom)

c := geom.CoordSeq()

fmt.Println(c)

Terminal output

go build -o build/sherlock && ./build/sherlock  sandbox
Loading plots
Loading data/plots/cadastre-33118-parcelles.json
Loaded  3195  plots
loaded  3195  plots
true
POLYGON ((-0.4582076000000000 44.7993021999999996, -0.4582887000000000 44.7993025000000031, -0.4583649000000000 44.7992983999999979, -0.4585742000000000 44.7992846000000000, -0.4593502000000000 44.7992338000000032, -0.4593561000000000 44.7992076999999966, -0.4596615000000000 44.7991846999999979, -0.4603783000000000 44.7991256000000035, -0.4605525000000000 44.7990909999999971, -0.4605585000000000 44.7990897999999973, -0.4608077000000000 44.7990404000000026, -0.4610179000000000 44.7990095999999980, -0.4614317000000000 44.7989012000000031, -0.4615591000000000 44.7988648999999981, -0.4617819000000000 44.7987836000000001, -0.4625367000000000 44.7984971999999999, -0.4628941000000000 44.7983564999999970, -0.4631426000000000 44.7982646000000031, -0.4635452000000000 44.7981599000000017, -0.4635047000000000 44.7981363999999971, -0.4633499000000000 44.7980465999999993, -0.4633121000000000 44.7980631000000002, -0.4630411000000000 44.7981709000000023, -0.4627151000000000 44.7982877000000030, -0.4623857000000000 44.7983916000000022, -0.4617932000000000 44.7985763000000006, -0.4614215000000000 44.7986914999999968, -0.4611998000000000 44.7988021999999972, -0.4609259000000000 44.7989229000000009, -0.4607376000000000 44.7989528000000021, -0.4605518000000000 44.7989934000000005, -0.4603233000000000 44.7990039999999965, -0.4602825000000000 44.7990013000000005, -0.4602601000000000 44.7989960999999965, -0.4602467000000000 44.7989887000000024, -0.4601216000000000 44.7990070000000031, -0.4596715000000000 44.7990593000000032, -0.4594695000000000 44.7990818999999973, -0.4592002000000000 44.7990998000000005, -0.4588552000000000 44.7991189000000034, -0.4585252000000000 44.7991371999999970, -0.4583039000000000 44.7991519999999994, -0.4577353000000000 44.7991896999999994, -0.4573378000000000 44.7992153000000002, -0.4569916000000000 44.7992416000000020, -0.4568799000000000 44.7992498999999995, -0.4568769000000000 44.7992502000000030, -0.4568471000000000 44.7992524000000003, -0.4563566000000000 44.7992984999999990, -0.4562820000000000 44.7993128999999968, -0.4561634000000000 44.7993355999999991, -0.4558317000000000 44.7994033000000016, -0.4554953000000000 44.7994802999999990, -0.4554610000000000 44.7994861000000029, -0.4554419000000000 44.7995512000000033, -0.4554218000000000 44.7996197000000009, -0.4554639000000000 44.7996106000000012, -0.4560579000000000 44.7994764999999973, -0.4561851000000000 44.7994472999999971, -0.4563304000000000 44.7994284999999977, -0.4564144000000000 44.7994211000000035, -0.4565135000000000 44.7994131999999965, -0.4566083000000000 44.7994088000000019, -0.4569515000000000 44.7993840999999975, -0.4573793000000000 44.7993562999999995, -0.4580135000000000 44.7993060000000014, -0.4581159000000000 44.7993013999999974, -0.4581695000000000 44.7993001999999976, -0.4582076000000000 44.7993021999999996))
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x102861c84]

goroutine 1 [running]:
github.com/twpayne/go-geos.(*Geom).CoordSeq(0x14000024100)
	/Users/paul/go/pkg/mod/github.com/twpayne/go-geos@v0.5.0/geom.go:56 +0xe4
sherlock/cmd.glob..func4(0x102d0bc40?, {0x1029afab9?, 0x0?, 0x0?})
	/Users/paul/projects/sherlock-import/cmd/sandbox.go:27 +0x160
github.com/spf13/cobra.(*Command).execute(0x102d0bc40, {0x102d41530, 0x0, 0x0})
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:920 +0x5b0
github.com/spf13/cobra.(*Command).ExecuteC(0x102d0b960)
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:1044 +0x35c
github.com/spf13/cobra.(*Command).Execute(...)
	/Users/paul/go/pkg/mod/github.com/spf13/cobra@v1.6.1/command.go:968
sherlock/cmd.Execute()
	/Users/paul/projects/sherlock-import/cmd/root.go:20 +0x28
main.main()
	/Users/paul/projects/sherlock-import/main.go:6 +0x1c

The structure I use for plots

type Plot struct {
	Id    string             `json:"id"`
	Geom  *geometry.Geometry `json:"geometry"`
	Props PlotProps          `json:"properties"`
}

type PlotProps struct {
	CityCode string `json:"commune"`
}

type PlotsJSON struct {
	Plots []Plot `json:"features"`
}

Everything seems fine to me and I have no error on compilation.
I have tried many thing to make it work:

  • load less data
  • defer runtime.GC()
  • try to run the method on different data

I am quite new to golang, do I miss an obvious issue with my code ?

Polygons don't have coordinate sequences, and this is triggering a bug in go-geos. I'll open a PR to fix this.

Polygon rings do have coordinate sequences. Did you mean the following instead of c := geom.CoordSeq()?

c := geom.ExteriorRing().CoordSeq()

I've tagged version 0.5.1 which includes the fix.

Polygons don't have coordinate sequences, and this is triggering a bug in go-geos. I'll open a PR to fix this.

Polygon rings do have coordinate sequences. Did you mean the following instead of c := geom.CoordSeq()?

c := geom.ExteriorRing().CoordSeq()

This solve my problem! Thank you so much for this quick answer and all the work you have done on this package!