Giter VIP home page Giter VIP logo

go-geom's People

Contributors

aabizri avatar bjamet avatar chrisdinn avatar dependabot[bot] avatar dmitriyvtitov avatar engelsjk avatar erikgrinaker avatar icholy avatar kostub avatar larsgh avatar otan avatar prashantkhoje avatar sams96 avatar twpayne avatar vcabbage avatar zzeekkaa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-geom's Issues

WKB is missing collections support

This WKB

0107000000030000000101000000550B36BFABD753C07CE58B07A5D24540010200000005000000C4190D2ABBD753C028AA6D799BD24540B51F84DBB5D753C07954A1269FD24540F9DB9E20B1D753C0E3D011AFA1D24540FB8E86F8ACD753C0ED444948A4D24540550B36BFABD753C07CE58B07A5D24540010200000002000000550B36BFABD753C07CE58B07A5D24540F6D786E5AAD753C0C619C39CA0D24540

is

GEOMETRYCOLLECTION(POINT(-79.3698576 43.6456613),LINESTRING(-79.3707986 43.6453697,-79.3704747 43.6454819,-79.370186 43.6455592,-79.3699323 43.6456385,-79.3698576 43.6456613),LINESTRING(-79.3698576 43.6456613,-79.3698057 43.6455265))

But fails with

wkb: unsupported type: 7

Unmarshal geoJSON []byte to MultiPoint

This fail at compile

	data := geom.NewMultiPoint(geom.XYZM)
	err := geojson.Unmarshal(readFile(), data)

With error:

cannot use data (variable of type *geom.MultiPoint) as type *geom.T in argument to geojson.Unmarshal:
*geom.MultiPoint does not implement *geom.T (type *geom.T is pointer to interface, not interface)

The only way I could do it was using type hint:

func readMultiPoint() *geom.MultiPoint {
	var g geom.T
	err := geojson.Unmarshal(readFile(), &g)
	if err != nil {
		panic(err)
	}
	return g.(*geom.MultiPoint)
}

Is there a way to make the geojson.Unmarshal to work with MultiPoint? (or other geom).

Polygon intersection and containment

I'm looking for a way to use this library to check, using Cartesian coordinates:

  • If two polygons intersect
  • if a polygon fully contains another polygon

I'm not seeing any exported functions for doing this. Is there any support for it, or should I look somewhere else?

From what I can tell, checking if a point is contained in a polygon can be accomplished this way:

func PolygonContainsPoint(polygon *geom.Polygon, p *geom.Point) bool {
	exterior := polygon.LinearRing(0)
	if !xy.IsPointInRing(geom.XY, p.Coords(), exterior.FlatCoords()) {
		return false
	}

	// Next rings are holes
	num := polygon.NumLinearRings()
	for i := 1; i < num; i++ {
		interior := polygon.LinearRing(i)
		if xy.IsPointInRing(geom.XY, p.Coords(), interior.FlatCoords()) {
			return false
		}
	}

	return true
}

Or? I'm not too familiar with rings or if this is how they're intended to be used. Either way, such a function would be a useful addition to this library, I think.

Error when trying to Scan an EWKB polygon

I am getting sql: Scan error on column index 0: unsupported Scan, storing driver.Value type []uint8 into type *geom.Polygon when trying to retrieve a geometry from my PostGIS database.

I'm doing a very simple Scan now like so:

rows, err := db.Query("SELECT geom FROM testgeoms")
for rows.Next() {
	var g gogeom.Polygon
	err := rows.Scan(&g)
}

Any pointers on what went wrong here?

Simplify GeoJSON + Rewrite FlatCoords

First of all, thanks for the library, it's really great and it's helping me a lot!

I'm trying to create an API that is going to load GeoJSON files, simplify them and then send this simplified version to the caller.
From what I understand, I can do the simplification using xy.SimplifyFlatCoords(feature.Geometry.FlatCoords(), .5, feature.Geometry.Stride()).
My problem is since it's returning the indexes that I should use, I would need to recreate the Feature, or replace the existing FlatCoords, but I don't seem to find a way to do this.

What's the best approach here?

Potential collision and risk from indirect dependence "github.com/gotestyourself/gotestyourself"

Background

Repo twpayne/go-geom used the old path to import gotestyourself indirectly.
This caused that github.com/gotestyourself/gotestyourself and gotest.tools coexist in this repo:
https://github.com/twpayne/go-geom/blob/master/go.mod (Line 13 & 21)

github.com/gotestyourself/gotestyourself v2.2.0+incompatible // indirect
gotest.tools v2.2.0+incompatible // indirect 

That’s because the gotestyourself has already renamed it’s import path from "github.com/gotestyourself/gotestyourself" to "gotest.tools". When you use the old path "github.com/gotestyourself/gotestyourself" to import the gotestyourself, will reintroduces gotestyourself through the import statements "import gotest.tools" in the go source file of gotestyourself.

https://github.com/gotestyourself/gotest.tools/blob/v2.2.0/fs/example_test.go#L8

package fs_test
import (
	…
	"gotest.tools/assert"
	"gotest.tools/assert/cmp"
	"gotest.tools/fs"
	"gotest.tools/golden"
)

"github.com/gotestyourself/gotestyourself" and "gotest.tools" are the same repos. This will work in isolation, bring about potential risks and problems.

Solution

Add replace statement in the go.mod file:

replace github.com/gotestyourself/gotestyourself => gotest.tools v2.3.0

Then clean the go.mod.

geom.Point must implement Encoder or be converted to a string

I'm new to go and gis so forgive probably a lot of ignorance. I'm using gorm as my ORM and trying to insert a 3D ECEF point into PostGIS like so

type Example struct {
  Location    *geom.Point    `json:"location" gorm:"type:geometry(POINTZ, 4978)"`
  // ...
}

type Input struct {
  Location    []float64
}

func (db *PostgresDB) LogExample(input *Input) error {
point, err := geom.NewPoint(geom.XYZ).SetSRID(4978).SetCoords(
	geom.Coord{input.Location[0], input.Location[1], input.Location[2]})
// handle error
obj := &Example{
	Location:    point,
}
return db.Create(&obj).Error

When I do this I get Cannot encode geom.Point into oid 18001 - geom.Point must implement Encoder or be converted to a string

What am I missing?

In encoding/geojson, if Geometry.Coordinates==nil, Geometry.Decode() panics.

Hey !

Given a nil Geometry.Coordinates, Geometry.Decode() panics. I found out about this via a fuzz test.
I applied a check on the calling side to abort before calling Decode if Geometry.Coordinates is nil as a quickfix.

Stack trace

See this stack trace: report.txt (I removed the calls in my code that leads up to the call to Geometry.Decode().

What I expected

I expected Geometry.Decode() to return an error.

What I saw instead

A panic panic: runtime error: invalid memory address or nil pointer dereference

encoding/wkb: POINT EMPTY encodes and decodes differently

It seems as if POINT EMPTY when encoded to a WKB with encoding/wkb and then decoded will error with EOF as it tries to read more points, but there not any (with ReadFloatArray). Unfortunately this presents an annoying behaviour where Encode(Decode('POINT EMPTY')) != "POINT EMPTY')

From #161 (comment), it appears there's no WKB compliant way of representing POINT EMPTY.

Here are a few options we can do:

  • Should POINT EMPTY error when decoding to a WKB?
  • Should we return a POINT EMPTY if there are 0 bytes left to read? (not sure that's possible as the io.Reader has no BytesRemaining which is kind of annoying)?
  • Should we introduce NaN, NaN syntax as previously put forward by #161?

I'm also for the argument that for EWKB, we should copy exactly what PostGIS does - after all, EWKB seems to be PostGIS specific. This backtracks my previous support for option 3 of #161 (comment).

More OGC-compliant method names?

Opening this issue for discussion.

#155 demonstrated a problem in this library where Empty() has different semantics for GeometryCollections vs. other geometry types. @otan proposed a fix for this in #156.

There is an opportunity to tidy up other aspects of go-geom in a v2 release:

Follow the Simple Feature Access methods as closely as possible in both name and type signature within the limits of Go's type system. Examples:

  • Empty() could be renamed to IsEmpty().
  • GeometryCollection.NumGeoms() could be renamed to GeometryCollection.NumGeometries().
  • The spec says that Point.M() should return a Double (float64) or NIL without defining what NIL is. Go does not have sum types, returning a *float64 would be both painful to use and have poor performance, but maybe returning a NaN would be sufficient.

The downside of following the SFA methods more closely would be a less idiomatic Go API and more verbosity in many cases.

We could also:

  • Make a clearer distinction between normal geometry types (Point, MultiPoint, LineString, MultiLineString, Polygon, and MultiPolygon) and compound types and GeometryCollection.

  • Possibly open the way to implementing representations of other geometry types, e.g. PolyhedralSurface and TIN.

@otan do you have any thoughts on this?

encoding/geojson should support null geometry for Feature

Based on spec geometry value in Feature can be JSON null. But current implementation panics.

I can make pull request but I'm not sure where implement it. In Geometry.Decode or in Feature.UnmarshalJson. Changes in Geometry.Decode can affect GeometryCollection I think.
Don't think it is right behavior.
But library should not panic is incorrect GeoJSON provided IMO.

WKT decoder could use re2c

re2c just got a Go backend, and looks like a nice little lexer for WKT. Maybe we could use this to provide more robust WKT decoding?

Use mapstructure to decode geometry

Hi,

I'm using your library to convert some mongodb documents into postgis rows.
The way I do it is simple, with mongodb I decode the bson into a struct with map[string]interface{},
Then I use your library to convert the geojson part into ewkb.

But to do so I have to first use json.Marshal, because the Unmarshal method from geojson needs a json (in bytes) to Unmarshal it.
So it's Bson -> map[string]interface{} -> json -> Geometry

Would it be possible to use mapstructure to encode/decode (using maybe separate functions to not break the API), to prevent this kind of pattern?
It should be pretty simple from my very short experience with mapstructure in the past.

add Invert function in xy package

A Invert function can enable the linear ring to transform from CCW to CW and vise versa, which is makes construct a proper polygon easier.

3D polygon area incorrect?

Hi there,

I'm trying to use Area() on 3D vertical polygons and the result is 0. I stepped through the code and it seems the Z coordinate is ignored. Is this how it's supposed to be? For example I'm using a poly with the following flat coords: [112,55,2,112,55,5,112,57,4.5,112,57,2,112,55,2], no holes, layout is XYZ. The Area returned is 0.

Thanks,
Gosia.

Question: dealing with postgis geometry type

My geo data are stored in a column of type GEOMETRY in postgis, because it can be a point, a line or a polygon.

Thus I wonder which encoding/wkb type should I use to decode its value when I fetch the data. Indeed for instance it can't be a wkb.Point because it will work only for a geometry of kind point, and I will have an error for other kinds.

Any help would be appreciated :)

Polygon.Area() result does not match with geojson.io value

When using the following GeoJSON

{
    "type": "Polygon",
    "coordinates": [
        [
            [
                -105.58281422838608,
                41.33023341916364
            ],
            [
                -105.58281408553712,
                41.33042554378588
            ],
            [
                -105.58244996842966,
                41.33042600155808
            ],
            [
                -105.58245011344185,
                41.33023404891613
            ],
            [
                -105.58281422838608,
                41.33023341916364
            ]
        ]
    ]
}

at geojson.io, produces area as:

Sq. Meters | 650.67
Sq. Kilometers | 0.00
Sq. Feet | 7003.76
Acres | 0.16
Sq. Miles | 0.00

When I modify polygon_test.go using the above coordinates:

func ExampleNewPolygon() {
	unitSquare := NewPolygon(XY).MustSetCoords([][]Coord{
		{
			{-105.58281422838608, 41.33023341916364},
			{-105.58281408553712, 41.33042554378588},
			{-105.58244996842966, 41.33042600155808},
			{-105.58245011344185, 41.33023404891613},
			{-105.58281422838608, 41.33023341916364}},
	})
	a := unitSquare.Area()
	fmt.Printf("unitSquare.Area() == %f", a)
	// Output: unitSquare.Area() == -0.0000000699242652662667
}

area calculated is -0.0000000699242652662667; not same as any of the numbers shown in GeoJSON.io.

Questions

  1. Why is the value different?
  2. What is the unit of area?
  3. How can I set the unit?

Wrong Number of Polygons in Multipolygon

When I unmarshal the following GeoJson Multipolygon and later call NumPolygons on it, I would expect to get 2 as result, but the result is 1, because endss is [[10 20]] instead of [[10] [20]]
{"type":"MultiPolygon","coordinates":[[[[0,35],[0,40],[10,40],[10,35],[0,35]],[[6,48],[6,51],[10,51],[10,48],[6,48]]]]}

Is this a bug or am I missing some important point ?

KML Decoding support

Is there any plan to support decoding of KML data, such that the package could be used to translate from KML to GeoJSON?

incorrect handling of GEOMETRYCOLLECTION Z/M/ZM EMPTY

At time of writing, GEOMETRYCOLLECTION handles layout by scanning through every item in the GEOMETRYCOLLECTION and finding the max layout.

For empty GEOMETRYCOLLECTION, Layout() would return NoLayout. This makes it impossible to differentiate between GEOMETRYCOLLECTION, GEOMETRYCOLLECTION Z, GEOMETRYCOLLECTION M and GEOMETRYCOLLECTION ZM.

There are a few solutions I can think of. This simplest would be to introduce a emptyLayout on GeometryCollection. The interface would be geom.NewGeometryCollectionEmpty(layout).

However, in PostGIS at least, GeometryCollections cannot have mixed dimensions. If that's something we also want to enforce, we really should be changing NewGeometryCollection to take in a layout argument and Push/MustPush only accepts the geometry if it is the same dimension, i.e. geom.NewGeometryCollection(geom.XY). This is an API change though so I expect this would be a major version bump.

We could have some sort of hybrid thing, where a layout argument to NewGeometryCollection is optional, where being not specified by default means it is NoLayout. Adding the first geometry would change the dimensionality accordingly (if it is NoLayout). Open question as to whether we support mixed-dimensions in this case though -- if not, this will not affect the API on upgrade and seems like a happy intermediate solution.

cc @twpayne for your thoughts.

creating buffer around point

Hi, I was just wondering if it would be possible to create a buffer around a point. How would you implement this with this library? This would be the equivalent of ST_Buffer

Restore convex hull functionality

Convex hull functionality was removed in #62 in response to #61.

From #61 (comment):

If you could contribute a replacement (the easiest way to do this is likely to find some BSD-compatible code and translate it to Go), that would be awesome! The only function that needs replacing is xy/internal/robustdeterminate/robust_determinate.go. You can then revert commit 2a0c7d6 to restore the functionality.

Question about the Valuer implementation of EWKB

Hi,

lately I've been trying to get this library to work with sqlboiler, to basically teach it how to talk to PostGIS. Everything went pretty well, but when attempting to INSERT or UPDATE, this error https://github.com/golang/go/blob/master/src/database/sql/convert.go#L194 kept popping up.

Everything looked good, I didn't understand why the Value isn't called. I found out this switch statement https://github.com/golang/go/blob/2ebe77a2fda1ee9ff6fd9a3e08933ad1ebaea039/src/database/sql/driver/types.go#L241 doesn't detect ewkb.GeometryCollection that I was using as a Valuer.

The fix I ended up doing is this: JanBaryla@daae2ee. Not quite sure what the consequences could be, though, so I'm opening an Issue instead of a PR.

type Box?

Hello,

Do you support type Box (x1,y1), (x2,y2) ?
Thanks.

geojsonFeature does not support number ids

According to RFC 7946 a feature can have a numeric identifier:

If a Feature has a commonly used identifier, that identifier
SHOULD be included as a member of the Feature object with the name
"id", and the value of this member is either a JSON string or
number.

When I try to load a file that uses numeric identifiers this currently results in an error, because geojsonFeature only accepts a string:

type geojsonFeature struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
BBox []float64 `json:"bbox,omitempty"`
Geometry *Geometry `json:"geometry"`
Properties map[string]interface{} `json:"properties"`
}

The area of some 2D polygons is incorrect

Hi there!

First of all, thank you for putting together this library, it's been a great help for the project I'm working on at the moment.

I think I've spotted an issue with area calculation of some 2D polygons with an interior ring. To caveat this, I'm entirely new to working with geospatial data, so let me know if you think otherwise. If you agree with the issue I'm happy to open a PR to fix this.

The problem

In my testing I found that the polygon I included in GeoJSON to the bottom of this issue, which has an exterior and an interior ring, has a larger area if you take into account the interior ring than if you consider the polygon without the cutout of the interior ring.

The issue is that the area of the interior ring is negative, so when subtracted from the area of the exterior ring the overall area is larger, which is incorrect.

I found on Wikipedia that the area of a polygon will be considered negative if it's negatively oriented. If that's the case, you can take the absolute value to get the area of the polygon.

If the polygon is negatively oriented, then the result A of the formulas is negative. In any case |A| is the sought area of the polygon.

Potential solution

Wrapping math.Abs around doubleArea on line 263 should fix the issue.

go-geom/flat.go

Lines 258 to 264 in 3538962

func doubleArea1(flatCoords []float64, offset, end, stride int) float64 {
var doubleArea float64
for i := offset + stride; i < end; i += stride {
doubleArea += (flatCoords[i+1] - flatCoords[i+1-stride]) * (flatCoords[i] + flatCoords[i-stride])
}
return doubleArea
}

Example polygon that's affected by this issue

{"type":"Polygon","coordinates":[[[119918.67718197108,485786.741805515],[119918.69838706788,485785.29893093504],[119918.88923284179,485783.2831503406],[119919.24971930924,485781.3734634614],[119919.77984644996,485779.4425579076],[119920.43720410654,485777.6601834242],[119921.30661262499,485775.8778090019],[119922.2636510625,485773.72258849995],[119921.73365106489,485773.42658845807],[119923.16265109007,485770.86958843237],[119923.80765108918,485771.1265884876],[119926.17865111862,485768.61858840234],[119947.56065127277,485770.90258856124],[119948.17345583934,485771.039935658],[119949.33973553519,485771.35821677576],[119950.80288642865,485771.90990408685],[119952.11760171659,485772.5464664185],[119953.68677802975,485773.5861849084],[119955.19233908955,485774.7956532407],[119956.74031031624,485776.4082777353],[119957.60971880952,485777.63896488526],[119958.37310187577,485778.8908706995],[119959.11527985494,485780.4822764615],[119959.64540698321,485781.8614948073],[119959.98468834005,485783.049744464],[119960.21794427553,485784.15311904956],[119960.34517478317,485785.6808686479],[119960.34517478317,485787.7603053907],[119959.96348321895,485790.1368047347],[119959.41215098523,485791.7494291324],[119958.43671705364,485793.2347411709],[119957.80056448438,485794.041053401],[119956.82513055089,485795.1019906065],[119954.30172537726,485797.6694585515],[119952.90218973215,485798.8789268689],[119951.2693981506,485800.130832687],[119948.97924891827,485801.82833215175],[119937.8206509836,485810.0995887836],[119934.1136509639,485808.8275887822],[119933.91665095915,485809.31858880114],[119930.12665093935,485807.5255888168],[119930.46165094808,485807.0345888033],[119929.17369913645,485806.30548697076],[119926.96837026457,485804.6928623607],[119925.46280921216,485803.2712066501],[119924.2329142613,485801.95564451686],[119923.42712102513,485800.9583636061],[119922.62132778516,485799.81255143834],[119921.83673963061,485798.56064560346],[119920.84010063173,485796.45999000553],[119920.05551248007,485794.2108032379],[119919.46177010139,485792.4496474469],[119919.058873486,485790.5611792903],[119918.74079722205,485788.54539868433],[119918.67718197108,485786.741805515]],[[119938.367,485783.35],[119938.4305,485783.4683],[119938.455,485783.514],[119938.672,485784.816],[119938.71029786766,485784.8850877253],[119938.718,485784.952],[119938.789,485785.085],[119938.6211,485786.9466],[119938.598,485787.203],[119938.62227495036,485787.2467909786],[119938.604,485787.377],[119939.2457241494,485788.5346430877],[119939.665,485789.402],[119939.72748391585,485789.5147183282],[119939.732,485789.531],[119939.85304413277,485789.7493584703],[119940.054,485790.945],[119940.08762402345,485791.0056563091],[119940.0923,485791.0865],[119942.51977688179,485795.46556510986],[119942.67047688179,485795.5700651099],[119942.80947688178,485795.50606510986],[119942.80747688179,485795.47406510985],[119942.98337688179,485795.4380651099],[119943.67637688179,485795.2961651099],[119944.11147688178,485795.20706510986],[119944.22107688179,485795.2163651099],[119944.25247688178,485795.2190651099],[119944.27887688178,485795.2079651099],[119944.39247688178,485795.1600651099],[119944.55237688178,485795.16786510986],[119946.48530164218,485795.2620817077],[119946.50247688178,485795.2930651099],[119946.66847688178,485795.3090651099],[119946.79547688179,485795.2140651099],[119946.77847688178,485795.1760651099],[119947.74317688179,485794.7005651099],[119948.05657688179,485794.5461651099],[119948.66947688178,485794.2440651099],[119948.80147688178,485794.2190651099],[119948.93347688178,485794.1550651099],[119948.92347688178,485794.12006510986],[119949.90877688178,485793.9028651099],[119950.23447688179,485793.8310651099],[119950.40847688178,485793.84306510986],[119950.42047688179,485793.62606510986],[119950.37790957323,485793.54927549465],[119950.10547688178,485792.26706510986],[119950.13947688178,485792.1320651099],[119950.12147688179,485791.9930651099],[119950.09238926535,485791.9405922863],[119950.17087688179,485790.0332651099],[119950.17747688178,485789.8730651099],[119950.25047688179,485789.7220651099],[119949.38759609578,485788.16546485043],[119949.13847688178,485787.67406510987],[119949.118377559,485787.63780678593],[119949.14147688179,485787.5670651099],[119949.08047688179,485787.45206510986],[119949.05097688179,485787.33346510987],[119948.76157688178,485786.1695651099],[119948.75647688178,485786.1490651099],[119948.79147688179,485786.1440651099],[119948.78347688178,485785.9690651099],[119946.356,485781.59],[119946.236,485781.525],[119946.049,485781.587],[119945.5663,485781.2557],[119945.1974,485781.0026],[119945.165,485780.9803],[119945.013,485780.876],[119944.9559,485780.8985],[119944.861,485780.936],[119944.749,485780.769],[119943.288,485781.4139],[119943.0598,485781.5146],[119942.377,485781.816],[119942.216,485781.752],[119942.072,485781.841],[119942.08718697626,485781.8683966597],[119940.19,485782.794],[119940.192,485782.824],[119940.1716,485782.825],[119940.039,485782.832],[119939.916,485782.907],[119939.905,485782.935],[119939.8406,485782.9473],[119939.1416,485783.0809],[119938.634,485783.178],[119938.636,485783.208],[119938.449,485783.219],[119938.367,485783.35]]]}

Reprojecting coordinates

I'm writing some code to reproject geometries. Is it safe to modify the flat coordinates like this?

func TransformCoords(g geom.T, transform func(geom.Coord) geom.Coord) {
	flatCoords := g.FlatCoords()
	stride := g.Stride()
	for i := 0; i < len(flatCoords); i += stride {
		coord := transform(flatCoords[i : i+stride])
		copy(flatCoords[i:], coord)
	}
}

wkb: unknown byte order: 1000111 (GeoPackage SQL Geometry Binary Format)

I want to insert and query Geometry(point) from GeoPackage.gpkg. When query, got this:

sql: Scan error on column index 0, name "geom": wkb: unknown byte order: 1000111

I don't know much about Geometry Binary Format. http://www.geopackage.org/spec121/index.html#gpb_format

It's there any way to solve the problem?

Code:

	db, e := sql.Open("sqlite3", "/home/x/公共的/Hubei.gpkg")
	if e != nil {
		log.Fatalln(e)
	}

	//查询
	rows, err := db.Query("select geom, name from points limit 1")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	for rows.Next() {
		var ewkbPoint ewkb.Point
		var name string
		err = rows.Scan(&ewkbPoint, &name)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("查到数据:", ewkbPoint.X(), ewkbPoint.Y(), name)
	}
	err = rows.Err()
	if err != nil {
		log.Fatal(err)
	}

GeoPackage DB(sqlite): https://file-1251456514.cos.ap-shanghai.myqcloud.com/geopackage/Hubei.gpkg

Unmarshalling a GeoJSON from openstreetmap

Hi, Thanks for the library. I'm new to dealing with geo related science. I am trying to use city boundary polygons from openstreetmap and do ops like find near by points and is point in a polygon.

We can download the GeoJSON dump from this link.
http://polygons.openstreetmap.fr/index.py?id=1766358

var geometry geom.T
if err := geojson.Unmarshal(waypoint, &geometry); err != nil {
	return err
}
_, ok := geometry.(*geom.GeometryCollection)

I am not sure what to convert the geometry to.

Could you please point me in the right direction ?

encoding/wkt: wkt decoding case and space insensitive

Trying to replace my WKT decoding from cgo-ing with GEOS with the go-geom one here.

Unfortunately, it doesn't work for a couple of cases, e.g.

  • POINT(1.0 1.0) should work (no space between POINT and ()
  • point (1.0 1.0) should work (case insensitive POINT)
  • POINTZ (1.0 1.0) should work (Z dimension / layout does not need a space afterwards)

Wondering if it's worth using a grammar file (https://godoc.org/golang.org/x/tools/cmd/goyacc) to do this instead.

I can work on this at a later date, but flagging as an issue for now. (It's not straightforward based on the way things are tokenised right now in the wkt decoder)

Linear Referencing

Linear Referencing is so useful

like posgis ST_LineInterpolatePoint

not find here

Alternate Postgres Driver

Is it possible to use pgx with go-geom ? I tried to alter the example using pgx and get the error

error ERROR: parse error - invalid geometry (SQLSTATE XX000)

It does work if I use database/sql and not pgx but my understanding is pgx supports more features and may have better performance.

func readGeoJSON(db *pgx.Conn) error {
pgon := {"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-111.650136,33.393364],[-111.650107,33.393521],[-111.650098,33.394139],[-111.650097,33.394242],[-111.650082,33.395449],[-111.650073,33.398822],[-111.650069,33.399323],[-111.650072,33.399718],[-111.65007199511102,33.39972009736757],[-111.64999199532836,33.39971991088767],[-111.64999199929186,33.39971821055429],[-111.64998900230722,33.399323607577415],[-111.64998900254967,33.399322361297806],[-111.6499930008521,33.39882157391809],[-111.65000200028477,33.39544878654093],[-111.650002006177,33.39544800587627],[-111.65001700482574,33.39424111460846],[-111.65001800377011,33.39413822333758],[-111.65001800848201,33.394137835074986],[-111.65002700848201,33.39351983507498],[-111.6500283308022,33.39350646874691],[-111.6500573308022,33.393349468746905],[-111.650136,33.393364]]]},"properties":{"Average":33,"Bearing":"N","Bucket":3,"County":"MARICOPA","FRC":4,"PostalCode":"85209","Reference":36,"RoadName":"S HAWES RD","ShapeSRID":4326,"Speed":37,"State":"ARIZONA","XDSegID":"121395201"}}
var poly Polygon

ctx := context.Background()
//if err := json.NewDecoder(r).Decode(&poly) ;err != nil {
//	return err
//}
err := json.Unmarshal([]byte(pgon), &poly)

if err != nil {
	logrus.Error(err)
}

if err != nil {
	logrus.Error("pytpe", err)
}
var geometry geom.T
if err := geojson.Unmarshal(poly.Geometry, &geometry); err != nil {
	fmt.Println(err)
}

polygon, ok := geometry.(*geom.Polygon)
if !ok {
	fmt.Println("geometry is not a point")
}

_, err = db.Exec(ctx, `
	INSERT INTO polygons(name, geom) VALUES ($1, $2);
`, "testinsert", &ewkb.Polygon{Polygon: polygon.SetSRID(4326)})

return err

}

Encoding and decoding functions for shapefiles

I've been doing some research to find a library for encoding/decoding shapefiles. I've found two:

  • go-shp
  • geom (Coincidentally the same name as this one)

go-shp only does the job of reading and writing files. Its objects do not provide a lot of functionality desired when dealing with geometry. So it kind of requires the work of transforming between library formats.

On the other hand, geom seems to be have a lot of the functionality desired for dealing with geometry, but it still lacks some of the functionality that go-geom has.

Are there any plans for implementing shapefile encoding/decoding? I think it would be a great additive :)

wkb: github.com/lib/pq requires binary_parameters=yes

I'm using the wkb package to query PostGIS via github.com/lib/pq. In the default configuration, the queries failed with pq: invalid byte sequence for encoding "UTF8": 0x00. I found that it was necessary to add binary_parameters=yes to the connection string. (Documented here https://godoc.org/github.com/lib/pq#hdr-Data_Types.)

Just wanted to open this issue in case someone else runs into the same problem. Maybe worth a note in the documentation?

Thanks!

runtime error: slice bounds out of range [:18] with capacity 16

While creating convexhull the function crashed ConvexHullFlat. Please find the stack trace along with sample test case. @twpayne

runtime/debug.Stack(0x210e2b8, 0xc0004d2a00, 0x2)
/usr/local/go/src/runtime/debug/stack.go:24 +0x9d
github.com/RichardKnop/machinery/v1/tasks.(*Task).Call.func1(0xc000245cc8, 0xc0004ba780)
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/tasks/task.go:130 +0xbc
panic(0x12819e0, 0xc0005009c0)
/usr/local/go/src/runtime/panic.go:679 +0x1b2
github.com/twpayne/go-geom/xy.(*convexHullCalculator).computeOctRing(0xc000244d38, 0xc000628000, 0x782, 0x782, 0xc000244c01, 0xc000244cb0, 0x49b521)
/home/talal/go/pkg/mod/github.com/twpayne/[email protected]/xy/convex_hull.go:242 +0x19b
github.com/twpayne/go-geom/xy.(*convexHullCalculator).reduce(0xc000244d38, 0xc000628000, 0x782, 0x782, 0x782, 0x782, 0xc000646000)
/home/talal/go/pkg/mod/github.com/twpayne/[email protected]/xy/convex_hull.go:176 +0x67
github.com/twpayne/go-geom/xy.convexHullCalculator.getConvexHull(0x1, 0x2, 0xc000628000, 0x782, 0x782, 0x8e30, 0x0)
/home/talal/go/pkg/mod/github.com/twpayne/[email protected]/xy/convex_hull.go:62 +0x2e2
github.com/twpayne/go-geom/xy.ConvexHullFlat(0x1, 0xc000628000, 0x782, 0x782, 0x2, 0x8e30)
/home/talal/go/pkg/mod/github.com/twpayne/[email protected]/xy/convex_hull.go:44 +0xa1
tespkg.in/seismic-service/pkg/set.(*traceIndexer3D).writeConvexHull(0xc0002c4000, 0x161aee0, 0xc0004cc680, 0x0, 0x0)
/home/talal/go/src/seismic-manager/pkg/set/segy_3d_indexer.go:794 +0x2ba
tespkg.in/seismic-service/pkg/set.(*traceIndexer3D).writeIndex(0xc0002c4000, 0x161aee0, 0xc0004cc680, 0x9, 0x0, 0x0)
/home/talal/go/src/seismic-manager/pkg/set/segy_3d_indexer.go:935 +0x257
tespkg.in/seismic-service/pkg/set.IndexSEGYSet(0x1632320, 0xc00046ad20, 0x7f57c25c7888, 0xc000044e40, 0xc00025ccc0, 0x1, 0x1, 0xc0000c0f00, 0xc0000f2b80, 0xc0005734b0, ...)
/home/talal/go/src/seismic-manager/pkg/set/index_segy_set.go:231 +0xdce
tespkg.in/seismic-service/server.(*seismicService).taskIndexSEGYSet(0xc000044420, 0x1632260, 0xc0000f2cc0, 0xc00025ccc0, 0x1, 0x1, 0xc000284000, 0x7d, 0xc00058c000, 0x63, ...)
/home/talal/go/src/seismic-manager/server/index_segy_set.go:237 +0x6da
reflect.Value.call(0x119f9c0, 0xc00007c090, 0x13, 0x1329133, 0x4, 0xc0000c0d20, 0x4, 0x4, 0x4, 0x18, ...)
/usr/local/go/src/reflect/value.go:460 +0x5f6
reflect.Value.Call(0x119f9c0, 0xc00007c090, 0x13, 0xc0000c0d20, 0x4, 0x4, 0x3, 0x3, 0xc00016e11c)
/usr/local/go/src/reflect/value.go:321 +0xb4
github.com/RichardKnop/machinery/v1/tasks.(*Task).Call(0xc0004ba780, 0x0, 0x0, 0x0, 0x7f57c2545558, 0xc0005009c0)
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/tasks/task.go:142 +0x25d
github.com/RichardKnop/machinery/v1.(*Worker).Process(0xc0000e78b0, 0xc000420000, 0x0, 0x0)
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/worker.go:169 +0x389
github.com/RichardKnop/machinery/v1/brokers/redis.(*Broker).consumeOne(0xc0004040c0, 0xc00028a800, 0x73f, 0x73f, 0x1619120, 0xc0000e78b0, 0x0, 0x0)
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/brokers/redis/redis.go:286 +0x241
github.com/RichardKnop/machinery/v1/brokers/redis.(*Broker).consume.func2(0xc0004040c0, 0xc00028a800, 0x73f, 0x73f, 0x1619120, 0xc0000e78b0, 0xc000408360, 0xc, 0xc0000a68a0)
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/brokers/redis/redis.go:250 +0x6a
created by github.com/RichardKnop/machinery/v1/brokers/redis.(*Broker).consume
/home/talal/go/pkg/mod/github.com/!richard!knop/[email protected]/v1/brokers/redis/redis.go:249 +0x153
INFO[0004] Failed processing task task_fae157f0-a03e-4504-9ff3-86636d5993c0. Error = runtime error: slice bounds out of range [:18] with capacity 16

geom_test.zip

Can not unmarshal WKB

This is a valid WKB (generated from PostgreSQL copy query):

package main

import (
	"encoding/hex"
	"log"

	"github.com/twpayne/go-geom/encoding/geojson"
	"github.com/twpayne/go-geom/encoding/wkb"
)

func main() {
	shape := "0103000020E610000001000000050000002CC5C594CC1453C01758A3D4190644402CC5E5FCCB1453C01B583BA31C06444029C59F81C91453C017580F581B0644402BC57F19CA1453C016583391180644402CC5C594CC1453C01758A3D419064440"
	decoded, err := hex.DecodeString(shape)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("%+v\n", decoded)
	wicket, err := wkb.Unmarshal([]byte(shape))
	if err != nil {
		log.Fatalln(err)
	}
	geoj, err := geojson.Marshal(wicket)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(geoj)
}

The WKT is

POLYGON((-76.3249866420126 40.0476632878855,-76.3249504321909 40.047748950936,-76.3247989712005 40.047709472172,-76.3248351810222 40.0476247311309,-76.3249866420126 40.0476632878855))

The PostGIS WKB from that WKT is

010300000001000000050000002FC5C594CC1453C01258A3D4190644402FC5E5FCCB1453C01E583BA31C06444028C59F81C91453C016580F581B06444028C57F19CA1453C011583391180644402FC5C594CC1453C01258A3D419064440

Regardless of which WKB, this library fails to unmarshal them.

Support for WKT

Hi,
Are you going to add support for encoding/decoding WKT as well?

Thanks,
Kostub

Determine WKT parsing strictness

This is opened from the comments from @otan and @andyyang890 in #197.

@otan wrote:

i think in go-geom, we want to relax things we check in CockroachDB (subject to @twpayne guidance!) as go-geom takes a more "OGC spec" view of the world, namely:
a) geometrycollections can have mixed dimensions.
b) linestrings/polygons do not have to have 2/4 points respectively, and polygon rings don't have to be closed.
c) i think empty can be mixed willy nilly in MultiPoint.

we can choose to optionally allow these checks (but have them off by default). this should be done with options -- see how we did the EncodeOption for Marshal above. probably DecodeOptionRunPostGISCompatibilityChecks which enables all the checks we did above.

@andyyang890 replied:

a) geometrycollections can have mixed dimensions.

If the PostGIS compatibility option is not specified:

  1. Should a GEOMETRYCOLLECTION{M, Z, ZM} be allowed to have geometries with different dimensions or does that only apply to GEOMETRYCOLLECTION?
  2. Should the layout for a GEOMETRYCOLLECTION always be geom.NoLayout?
  3. If the answer to 1 is no, should the layout of a *GeometryCollection be set if it is a GEOMETRYCOLLECTION{M, Z, ZM} or should it remain as geom.NoLayout?

Add examples

I want to use go-geom with postgis and postgres. But I didn`t find examples.
Can I use it with postgis?

Should I use ewkb (ST_AsEWKB) for your types of polygon and point or I should do something else?

Please add more examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.