Best way to store geojson data

2,156 views
Skip to first unread message

Jordan Thomas

unread,
Sep 2, 2014, 4:43:08 PM9/2/14
to loopb...@googlegroups.com

New to loopback but having fun setting up my first node based RestAPI. I am able to create models and have the associated tables created in the datastore. My models need to have properties which are data type geojson, a string of the form:

{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [125.6, 10.1] } }

Some are points. Others polylines or polygons. My question is, what is the best way to configure geojson properties in the model schema. I could just set the data type to string I guess but I am hoping there is something more elegant. Ideally I would like to setup custom validation that will check that submitted data is valid geojson. Also I need to be able to query the models by location, e.g. return data within lat/lng bounds.

I choose MYSQL as the datastore for now but only because of past experience. If another data source would be preferable I'll change.

Raymond Feng

unread,
Sep 2, 2014, 4:49:22 PM9/2/14
to Jordan Thomas, loopb...@googlegroups.com
There are a few choices.

1. For NoSQL DB such as mongodb, you can store the object directly as subdocument.
2. For relational databases such as MySQL, you can define the property type as Object. LoopBack will store them as TEXT/CLOB.
3. If the DB has native types for geo related data, we can potentially enhance the type mapping for connectors to take advantage of them.
 
Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jordan Thomas

unread,
Sep 2, 2014, 4:54:20 PM9/2/14
to loopb...@googlegroups.com, jor...@everyvine.com
Ok. I'm going to try going down the property type as Object path. Where can I find more info/example of adding my own validation logic to a model? I see in my models .json file there is a validations property. Any examples of putting custom functions in there?

Sumitha Nathan

unread,
Sep 2, 2014, 5:40:34 PM9/2/14
to loopb...@googlegroups.com, jor...@everyvine.com
Have you looked at the docs here

http://docs.strongloop.com/display/LB/Validating+model+data

Are you looking to add something custom beyond what's offered in the http://docs.strongloop.com/display/LB/Validations+class

Jordan Thomas

unread,
Sep 2, 2014, 7:09:07 PM9/2/14
to loopb...@googlegroups.com, jor...@everyvine.com
Thanks for the doc links a they do seem like what I want. I don't know how to implement what they describe however. For that matter, under validation when the doc says:
 
"This is not yet implemented. You must currently validate in code"

I am not sure what is meant by "in code". Where does this code live? An example of implementing custom validation on model properties would be wonderful.

Jordan Thomas

unread,
Sep 3, 2014, 1:22:53 PM9/3/14
to loopb...@googlegroups.com, jor...@everyvine.com
Those docs and particularly http://docs.strongloop.com/display/LB/Validations+class helped a lot. Thanks so much!


On Tuesday, September 2, 2014 2:40:34 PM UTC-7, Sumitha Nathan wrote:

Basil Titovchenko

unread,
Oct 2, 2014, 8:29:32 PM10/2/14
to loopb...@googlegroups.com, jor...@everyvine.com
Type mapping for PostGIS data types in PostgreSQL connector will be greatly appreciated. All of us, PostGIS users, need is a way to retrieve the value of ST_AsGeoJSON(the_geom) into model property value and store object value of property in DB table as ST_GeomFromGeoJSON(<geojson_object>). I didn't find a simple solution how to make it in current LoopBack ORM implementation, but hope it will be done in near future. Thanks in advance. ;)

Jordan Thomas

unread,
Oct 3, 2014, 1:49:11 PM10/3/14
to loopb...@googlegroups.com, jor...@everyvine.com
Yep, would love to see this too.

Julian Mayorga

unread,
Oct 4, 2014, 12:42:10 PM10/4/14
to loopb...@googlegroups.com
+1 I would also find this very helpful

Jon Duckworth

unread,
Aug 27, 2015, 9:53:35 AM8/27/15
to LoopbackJS, mayorga...@gmail.com
+1 

I am also looking for a good way to define PostGIS geometry types via Loopback. I wont' necessarily be fetching the data as GeoJSON because I'll have a proxied Geoserver instance, but I would like the geometries to be embedded within the models they are associated with, so I am looking for a way to define the geometry column as a property of the model at boot time. I may also eventually allow posting and editing of geometries, so that's another reason I am hoping to have the geometries embedded in the models rather than as a separate tables.

Are there any plans to develop a mapping to the PostGIS geometry type for the PostgreSQL connector?

Thanks.

Dror Matalon

unread,
Aug 27, 2015, 10:57:42 AM8/27/15
to loopb...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Jon Duckworth

unread,
Aug 27, 2015, 11:52:15 AM8/27/15
to LoopbackJS
Thanks for the suggestion. I thought about using that, but some of my geometries are going to be lines and polygons and I also want to be able to take advantage of PostGIS's spatial analysis capabilities, so I'd like to use their geometry type.

mathieu tanguay

unread,
Jan 20, 2016, 6:47:04 PM1/20/16
to loopb...@googlegroups.com
Is there any development on this topic ?
I'm trying to use postgis geometry type with loopback but i end up writing plain sql queries in remote methods like this...

var ds = TmTrack.dataSource;
var sql = util.format('SELECT id, name, description, favorite, altitude, maxspeed, averagespeed, applicationid, activityid, userid,ST_AsGeoJSON(geom) as geom, starttime, endtime, createdat, updatedat from tmtrack where userid=%s and applicationid=%s and activityid=%s;', userId, appId, activityId);

ds.connector.query(sql, [], function (err, tracks) {
    if (err) console.error(err);
    cb(err, tracks);
});

How do you guys do this ?

Thanks
Mat
Reply all
Reply to author
Forward
0 new messages