从Spatialite中获取多边形类型的数据并将其作为字符串返回,这是否可能?

huangapple go评论81阅读模式
英文:

Is it possible to get multipolygon type data as string from spatialite?

问题

sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"mod_spatialite",
},
})

db, err := sql.Open("sqlite3_with_extensions", "./map.gpkg")
if err != nil {
panic(err.Error())
}

query := "select AsText(geom) from level0"
rows, err := db.Query(query)
if err != nil {
panic(err.Error())
}

for rows.Next() {
var s []byte
rows.Scan(&s)
fmt.Print(s)
}

首先,你的代码如上所示。你想要以文本形式接收 geom 数据,例如 multipolygon((((......))),但是只返回了一个空数组。

下面是数据库的样子,数据以 multipolygon 类型存储。

我想要的是像下面图片中那样的文本形式,而不是上面的数据类型,但是我不知道该怎么做。

我已经加载了扩展的 spatialite 到 sqlite3,并通过 astext 加载了数据,但是返回的是一个空数组 [],[],[]。我该如何返回我想要的 multipolygon 文本呢?任何帮助将不胜感激。

英文:
sql.Register("sqlite3_with_extensions",
	&sqlite3.SQLiteDriver{
		Extensions: []string{
			"mod_spatialite",
		},
	})

db, err := sql.Open("sqlite3_with_extensions", "./map.gpkg")
if err != nil {
	panic(err.Error())
}

query := "select AsText(geom) from level0"
rows, err := db.Query(query)
if err != nil {
	panic(err.Error())
}

for rows.Next() {
	var s []byte
	rows.Scan(&s)
	fmt.Print(s)
}

First of all, the code I wrote is as above, I want to receive the geom data as text like multipolygon((((......))) , but only an empty array is returned.

Below is what the database looks like, data is stored in multipolygon type
从Spatialite中获取多边形类型的数据并将其作为字符串返回,这是否可能?

Loading this data into golang returns data like the picture below从Spatialite中获取多边形类型的数据并将其作为字符串返回,这是否可能?

I want text like multipolygon ((((......))) , not datatype like above, but I don't know how to do it,

I have loaded extended spatialite into sqlite3 and loaded data through astext, but an empty array is returned as [], [], [] . How can I return the multipolygon text I want? Any help would be appreciated

答案1

得分: 1

请尝试这个代码:

package main

import (
	"database/sql"
	"log"

	"github.com/twpayne/go-geom/encoding/wkb"
	"github.com/twpayne/go-geom/encoding/wkt"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./gadm36_levels.gpkg")
	if err != nil {
		log.Fatal(err)
	}

	rows, err := db.Query("select geom from level0 limit 1")
	if err != nil {
		log.Fatal(err)
	}

	defer rows.Close()

	for rows.Next() {
		var b []byte
		var mp wkb.MultiPolygon

		if err := rows.Scan(&b); err != nil {
			log.Fatal(err)
		}

		if err := mp.Scan(b[40:]); err != nil {
			log.Fatal(err)
		}

		log.Printf("%+v", *mp.MultiPolygon)

		text, err := wkt.Marshal(mp.MultiPolygon)
		if err != nil {
			log.Fatal(err)
		}

		log.Printf("%v\n", text)
	}
}

40是WKB块的偏移量

希望对你有帮助!

英文:

Try this:

package main

import (
	"database/sql"
	"log"

	"github.com/twpayne/go-geom/encoding/wkb"
    "github.com/twpayne/go-geom/encoding/wkt"


	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db, err := sql.Open("sqlite3", "./gadm36_levels.gpkg")
	if err != nil {
		log.Fatal(err)
	}

	rows, err := db.Query("select geom from level0 limit 1")
	if err != nil {
		log.Fatal(err)
	}

	defer rows.Close()

	for rows.Next() {
		var b []byte
		var mp wkb.MultiPolygon

		if err := rows.Scan(&b); err != nil {
			log.Fatal(err)
		}

		if err := mp.Scan(b[40:]); err != nil {
			log.Fatal(err)
		}

		log.Printf("%+v", *mp.MultiPolygon)

        text, err := wkt.Marshal(mp.MultiPolygon)
        if err != nil {
                log.Fatal(err)
        }

        log.Printf("%v\n", text)
	}
}

40 is offset of WKB block.

huangapple
  • 本文由 发表于 2022年1月25日 00:35:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/70837250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定