twpayne / go-geom

Package geom implements efficient geometry types for geospatial applications.
BSD 2-Clause "Simplified" License
839 stars 104 forks source link

Alternate Postgres Driver #211

Closed gmonk closed 2 years ago

gmonk commented 2 years ago

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

}

twpayne commented 2 years ago

Yes, go-geom is neutral with respect to the database driver. You should be able to adapt the existing PostGIS example to use pgx. I think the only thing you need to change is to move from pg.CopyIn to pgx.CopyFrom for bulk import of data. No other changes should be needed.

gmonk commented 2 years ago

Sorry I was probably updating my comment as you were replying...What about inserts I attempted to replace database/sql with pgx and got the following error ERROR: parse error - invalid geometry (SQLSTATE XX000) above is the code i used ... however it does work using database/sql

twpayne commented 2 years ago

OK. The code you provide is not sufficient to reproduce the problem, for example it does not contain the code to create the database. Also, it does not create the table which you want to insert the geometry into. Also, the error report does indicate which particular function is returning the invalid geometry error.

Please can you you give exact steps to reproduce the problem on an empty machine.

twpayne commented 2 years ago

Closing due to inactivity.