GeoJSON.js icon indicating copy to clipboard operation
GeoJSON.js copied to clipboard

Add support for nested attribute properties

Open hparkertt opened this issue 5 years ago • 6 comments

The goal for this enhancement is to provide the ability to 'flatten' properties from the input into primitive attributes. For example:

data = {
  lat: 12.34,
  lng: 56.78,
  info: {
    name: 'Johnny Goode',
    age: 12
  }
}

GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']});

There is currently no way to get the 'name' property as an attribute of the resulting GeoJSON object.

In an attempt to avoid breaking existing logic, I think this may be a good solution:

function getPropFunction(params) {
  ...
  else if (params.extract) {
    func = function(properties) {
      ...
    }
  } else if (params.include) {
  ...
}

hparkertt avatar Jul 13 '20 20:07 hparkertt

Thanks for opening this.

Specifying extract would mean to dig into a nested property and elevate it on the output? In the case of duplicate key names, it may make sense to specify the full path, for example info.name.

caseycesari avatar Jul 14 '20 13:07 caseycesari

Hey Casey,

Yes, the intent is to elevate it on the output. I think the risk of duplicate key names should be resolved a different way. Although we could use the full path name as the new property name, that would make the resulting properties object very challenging to use.

To resolve that issue, we could either say 'tough luck, you need to make sure your property names are unique', we could change the extract option from an array to a dictionary, or we could allow both options. For example:

inputOptions1 = {
  Point: [ 'lat', 'lng' ],
  extract: [ 'info.name', 'info.age' ]
};

inputOptions2 = {
  Point: [ 'lat', 'lng' ],
  mapProperties: {
    'info.name': 'name',
    'info.age': 'age'
  }
};

hparkertt avatar Jul 14 '20 13:07 hparkertt

That makes sense. The mapProperties option seems best since it's most flexible, and would allow users to handle duplicate keys in a nice way.

caseycesari avatar Jul 14 '20 13:07 caseycesari

so...how to get nested attribute properties to the properties field in geoJSON sir? I cant find instruction in document :dancing_men: For example, if I want to add time field from announcedTime to properties in geoJSON, how can I do that sir?

{ _id: 60e63c323959b25c2609da46,
    name: 'BN0004',
    yearOfBirth: 2000,
    currentLocation:
     { _id: 60e63c323959b25c2609da40,
       lat: 10.77315,
       long: 106.65753,
       detailAddress:
        '354/1/3 Đường Lý Thường Kiệt, Phường 14, Quận 10, Thành Phố Hồ Chí Minh',
       district: 'Quận 10',
       __v: 0 },
    announcedTime:
     { _id: 60e63c323959b25c2609da42,
       time: 2021-02-28T23:43:00.000Z,
       __v: 0 },
    hasDisease: 60e546662a788a8fd024e202,
    __v: 0 },

Thank you!

longbuibao avatar Jul 08 '21 08:07 longbuibao

@longbuibao Hi there! This functionality has not been implemented yet. I found a different solution to this problem in my code and did not circle back to this enhancement. If you want to try and implement this, I'm sure @caseycesari would welcome a PR!

hparkertt avatar Jul 08 '21 13:07 hparkertt

I will try sir! :') I'm new to programming so I think I dont have enough experience to read code :')

btw my solution is pull out the property..thank you for great npm module!

longbuibao avatar Jul 08 '21 15:07 longbuibao