Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Geometry data. #414

Open
xiaoyusilen opened this issue May 19, 2017 · 6 comments
Open

How to get Geometry data. #414

xiaoyusilen opened this issue May 19, 2017 · 6 comments

Comments

@xiaoyusilen
Copy link
Contributor

(。・∀・)ノ゙Hi, I'm using rethinkDB to storage geometry data. But when I want to get data from rethinkdb, I got trouble.
My code:

		resp, err = r.DB(db).Table(table).Filter(map[string]string{
			"id": id,
		}).Field("gps").Field("location").Run(c)

		var re interface{}

		err = resp.One(&re)

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

I got data like below, type was nil,

{Point {116.77940333333333 39.948335} [] []}

I change the type of re to types.Geometry, error message I got like this,

gorethink: could not decode type types.Geometry into Go value of type *types.Geometry: pseudo-type GEOMETRY object is not valid

Or, how to use ToGeoJSON?
Thx~ @dancannon

@xiaoyusilen
Copy link
Contributor Author

I try to change data type.

result, err := re.(types.Geometry).MarshalRQL()

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

	log.Info(result)

I got error like,

panic: interface conversion: interface {} is types.Geometry, not types.Geometry

@iKonrad
Copy link

iKonrad commented May 19, 2017

Can you post your types.Geometry struct?
I don't think you need to play around with MarshalRQL in your case.

First of all, try changing:
var re interface{}
to
var re map[string]interface{}

and see if you can pull the raw data.

Next, if you want to automatically convert your map to Geometry object, you need to add gorethink tags in your struct declaration, like so:

package types
import "time"

type User struct {
	Id       string `gorethink:"id,omitempty"`
	Name     string `gorethink:"name"`
	Email    string `gorethink:"email"`
	Created time.Time `gorethink:"created"`
}

When that's done, you should be able to replace var re interface{} with var re types.Geometry and pass it to the .One() function just fine.

@xiaoyusilen
Copy link
Contributor Author

@iKonrad Thanks~
My data struct like,

"gps": {
	"created_at": Fri May 19 2017 15:01:51 GMT+08:00 ,
	"location": {
		"$reql_type$":  "GEOMETRY" ,
		"coordinates": [
			116.48641 ,
			39.933886666666666
		] ,
		"type":  "Point"
	} ,
	"updated_at": Fri May 19 2017 15:01:51 GMT+08:00
} 

I add tag like this,

type Position struct {
	GPS     Gps         `gorethink:"gps"`
}

type Gps struct {
	CreatedAt time.Time   `gorethink:"created_at"`
	UpdatedAt time.Time   `gorethink:"updated_at"`
	Location  interface{} `gorethink:"location"`
}

If I use interface{} as type of Location, it can get data, but then how I change Location to types.Point? Type of my data was nil.

@iKonrad
Copy link

iKonrad commented May 19, 2017

You need to specify the right struct for the Location in your Gps struct as well, so gorethink knows how to convert your objects:

type Gps struct {
	CreatedAt time.Time   `gorethink:"created_at"`
	UpdatedAt time.Time   `gorethink:"updated_at"`
	Location  Point `gorethink:"location"`
}

@xiaoyusilen
Copy link
Contributor Author

@iKonrad Sorry, I got error like below,

err is: gorethink: could not decode type interface {} into Go value of type *types.Point: pseudo-type GEOMETRY object is not valid

@mehmetkose
Copy link

same here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants