realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Transform GeoJSON object to a realm object

Open tdangexo opened this issue 2 years ago • 3 comments

Problem

I am working with GeoJSON data, here is an example:

"outline": { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Polygon", "coordinates": [ [ [ 2.6581091091385147, 49.44587585244173 ], [ 2.6632589504471085, 49.4464059894905 ], [ 2.6648897335281636, 49.445317807250234 ], [ 2.6633018657913468, 49.443280888399926 ], [ 2.6581091091385147, 49.44587585244173 ] ] ] }, "properties": { "name": "An example" } } ] }

I want to save this data as a realm object of type Outline. I have tried to create an embedded model, i.e, Geometry > Feature > Outline, but the validation of realm schema failed when it came to "coordinates". It seems that realm does not support schema property of array of array of array.

Solution

Are there any alternatives to deal with GeoJSON data?

How important is this improvement for you?

I'd like to see it, but have a workaround

tdangexo avatar Aug 16 '22 14:08 tdangexo

@tdangexo Thanks for the report. Can you share the Realm schema(s) you're working with?

fronck avatar Aug 17 '22 08:08 fronck

Hello @fronck, here are my schemas:

User

static schema = {

    name: 'User',
    primaryKey: 'id',
    properties: {
      id: 'int',
      email: 'string',
      first_name: {type: 'string?', default: ''},
      last_name: {type: 'string?', default: ''},
      company_name: {type: 'string?', default: ''},
    },
  };

Farm

 static schema = {
    name: 'Farm',
    primaryKey: 'id',
    properties: {
      id:'int',
      name:'string',
      user:'User',
      address:'string',
      technical_contact:'string'
    },
  };

Outline

 static schema = {
    name: 'Outline',
    properties: {
      type:'FeatureCollection["type"]',
      features:'FeatureCollection["features"]'
    },
  };

As I mentionned above, Outline has specific properties since the data is of type GeoJSON. Here, FeatureCollection is a specific type/interface imported from geojson.

tdangexo avatar Aug 17 '22 15:08 tdangexo

I can see the value in adding some utility functions to make it easier to work with GeoJSON but currently we don't have it on the agenda.

If type and features in Outline can be different kind of objects, mixed might be a good Realm type for it.

kneth avatar Sep 01 '22 16:09 kneth

@tdangexo Product for Realm & Device Sync here. I am interested in building this feature - I'd love to pick your bring on your use case if you're keen. If you could drop me a line at [email protected] and we can set up a time to chat.

ianpward avatar Nov 10 '22 18:11 ianpward

For people interested in geospatial objects and queries, it might be interesting to follow https://github.com/realm/realm-core/pull/6352

kneth avatar Mar 28 '23 08:03 kneth

v12.0.0-rc.0 contains geospatial queries and guidelines on how to transform data will be published.

kneth avatar Jun 30 '23 12:06 kneth