In ol3, having a GeoJSON with one of the properties called geometry fails:
var geojsonObject = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [102.0, 0.5]
},
"properties": {
"prop0": "value0",
"geometry": "This is a point"
}
}]
};
features = new ol.format.GeoJSON().readFeatures(geojsonObject);
console.log(features[0].getGeometry());
console.log(features[0].get('prop0'));
Is this legal? Should it be supported?
Yes, having a feature property named geometry is valid GeoJSON. The GeoJSON specification explicitly states that the
value of the properties member is an object (any JSON object or a JSON null value)
OpenLayers 3 stores the geometry as a normal property on a Feature, with the default name geometry. When reading the feature in your question, the geometry GeoJSON feature member is first written to the geometry property, but then overwritten by the geometry GeoJSON property member.
To avoid this collision, the geometry must be stored with a different property name. You can control that with the geometryName option to ol.format.GeoJSON:
var geoJSONFormat = new ol.format.GeoJSON({
geometryName: 'actualGeometry'
});
geoJSONFormat.readFeatures(geojsonObject);
Related
I am processing messages. Each message corresponds to a coordinate, and MOST of the coordinates are in a specific JSON form. Problem is, some naughty messages have the wrong format (a different format, where a whole list of coordinates comes in one message).
Ideally, I would like to process both types of message, but the built-in Parse function for JsonProvider seems to succeed even when the incoming data is in the wrong format. How can I know in advance (before attempting to parse) what format my datum is in?
My two data models look something like this:
type ModelA = JsonProvider<"""{
"id": "13222",
"timestamp": 1499329186332.0,
"latitude": 12.125419,
"longitude": 15.054884
}""">
type ModelB = JsonProvider<"""{
"data": {
"positions": [
{
"id": "13223",
"timestamp": 1499329186332.0,
"latitude": 12.125419,
"longitude": 15.054884
},
{
"id": "13223",
"timestamp": 1499329186332.0,
"latitude": 12.125419,
"longitude": 15.054884
}]
}
}""">
In a JSON model coming from a REST api I have a field that is supposed to be a Dictionary (a map, hashtable, call it whatever you want), e.g.:
"moduleMap": {
"677e55b2-d80c-4b32-bcbb-e99074fbfcf6": {
"id": "677e55b2-d80c-4b32-bcbb-e99074fbfcf6",
"startTime": 1496054599227,
"status": "ACTIVE"
},
"c20acde2-639f-4cb7-9b90-6d8d24c78166": {
"id": "c20acde2-639f-4cb7-9b90-6d8d24c78166",
"startTime": 1496054598997,
"status": "UNAVAILABLE"
}
}
As I understand RestKit (I am a newbie), usually this is mapped to an object in Swift. However, since the keys in this map are arbitrary uuids, I cannot write a class with these properties.
Can anybody point me to a direction of how to get this map into a Swift dictionary using RestKit mapping?
I would like to map it to var moduleMap: [String:DomainObject], or at least var moduleMap: NSMutableDictionary. Also, I need it to be mappable back to the same JSON.
I have given JSON and cannot parse partial data. It seems dictionary into dictionary:
{
"products": [
{
"id": 6796,
"title": "my title",
"description": "desc",
"code": "12345",
"valueType": "null",
"discounts": [
{
"minPrice": null,
"maxPrice": null,
"value": 20,
"avail": false
}
]
}
]
}
I am using the latest version of RESTKit but I cannot properly parse under discounts.
my RestKit settings are:
responseMapping.addAttributeMappingsFromDictionary([
"id" : "id",
"code" : "code",
"title" : "title",
"valueType" : "valueType",
"description" : "desc",
"discounts.minPrice" : "minPrice",
"discounts.maxPrice" : "maxPrice",
"discounts.value" : "value",
"discounts.avail" : "avail",
])
but all values below discounts always return Nil. What I am doing wrong?
You can't directly map using discounts.XXX because discounts is an array and you have no way to index into that array and extract a single value.
You either need to change the source JSON to compress the values out of the dictionary, or create a custom object that you can map each item in the discounts array to.
Technically you could map the whole discounts array, which would give you an array of dictionaries, that you could then unpack in the setter method, but the array of custom objects is usually a better approach.
I receive JSON code that looks like this:
{
"rent": {
"items": [
],
"total": 0
},
"upcoming": {
"items": [
],
"total": 0
},
"watchnow": {
"items": [
],
"total": 0
}
}
I want the keys, i.e. "rent", "upcoming" and "watchnow" as a property on the mapped object, so I add a NSString property called searchSection to the class I'm using and then create this mapping:
RKObjectMapping *searchResultsMapping = [RKObjectMapping mappingForClass:[TDXSearchResults class]];
[searchResultsMapping addAttributeMappingFromKeyOfRepresentationToAttribute:#"searchSection"];
Shouldn't my mappingResult.array then contain three TDXSearchResults objects, each with either "rent", "upcoming" or "watchnow" in its searchSection property? I only get one TDXSearchResults in the array and this confuses me greatly.
You should only expect 1 result because your source data is only 1 object (and you must only have 1 response descriptor).
If you want 3 result objects then you should use either:
forceCollectionMapping on the mapping
3 response descriptors, each with the same mapping for items and each using a different keypath (rent / upcoming / watchnow).
I am having trouble understanding how to Map location Data stored in Mongo DB with RestKit.
Here is the data I will be mapping in JSON
{ "name" : "TestPoint2", "media_resource" : "tester", "added" : ISODate("2012-10-10T23:00:00Z"), "loc" : { "type" : "Point", "coordinates" : [ -33.1, 12.54 ] }, "comments" : [ ], "tags" : [ ]}
The main point I am struggling with is mapping the "coordinates" of "loc"
To begin with, I'd treat the coordinates as they are represented in the JSON and store them as numbers in an array. If your object / entity has an array property / transformable attribute then you can just map the coordinates into that wholesale. You can then add accessor methods / transient attribute(s) which access that coordinate information and provide a nicer interface to get the lat/long than indexing into an array.