Inserting and selecting PostGIS Geometry with Gorm

后端 未结 4 1769
星月不相逢
星月不相逢 2021-02-07 15:56

I\'ve been trying to find a way to insert and retrieve geometric types using Golang, and specifically the library gorm. I\'m also attempting to use the library orb that defines

4条回答
  •  -上瘾入骨i
    2021-02-07 16:29

    The solution I ended up using was as follows:

    First I created new types that wrapped all of the orb types, for example:

    type Polygon4326 orb.Polygon
    type Point4326 orb.Point
    

    Then I implemented the Scan(), Value() methods on each type. I had to however edit the bytes and convert to/from hexadecimal. When you directly query on a spatial column in PostGIS, it will return a hexadecimal representation of EWKB, essentially WKB, but including 4 bytes to represent the projection ID (in my case 4326).

    Before inserting, I had to add the bytes that represent the projection of 4326.

    Before reading, I had to strip those bytes, since orb's built in scanning expected WKB format.

提交回复
热议问题