Related
I'm reading a GeoJSON file and importing the polygons (and other stuff) into mapbox-gl draw using draw.set(geoJSON). How do I color individual polygons by an attribute in the properties of a feature. Example:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
//"id": "the most unique id in the world",
"properties": {
"class_id": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.30961608886719,
61.57192958204744
],
[
79.34309005737303,
61.57192958204744
],
[
79.34309005737303,
61.57871162332267
],
[
79.30961608886719,
61.57871162332267
],
[
79.30961608886719,
61.57192958204744
]
]
]
}
},
{
"type": "Feature",
"properties": {
"class_id": 2
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.35201644897461,
61.58271478278019
],
[
79.35115814208984,
61.573972521656835
],
[
79.38188552856444,
61.57192958204744
],
[
79.35201644897461,
61.58271478278019
]
]
]
}
},
}
The idea is that we color the features with class_id = 1 as red, class_id = 2 as blue, and class_id = 3 as green. How do we do that?
You need set userProperties to true for the properties of a feature will be available for styling. And use prefix user.
And use case expression:
var Draw = new MapboxDraw({
userProperties: true,
styles: [{
'id': 'gl-draw-polygon-fill-inactive',
'type': 'fill',
'filter': ['all', ['==', 'active', 'false'],
['==', '$type', 'Polygon'],
['!=', 'mode', 'static']
],
'paint': {
'fill-color': [
"case",
['==', ['get', "user_class_id"], 1], "#00ff00",
['==', ['get', "user_class_id"], 2], "#0000ff",
'#ff0000'
],
'fill-outline-color': '#3bb2d0',
'fill-opacity': 0.5
}
}...
[ http://jsfiddle.net/5Lotf4ka/ ]
I'm working on PHP application that retrieves data from Adform API every day and saves them to Database.
There is a problem that data queried using dimensions, for example date and banner do not match totals, retrieved without dimensions.
For example, I make a POST to https://api.adform.com/v1/reportingstats/agency/reportdata
{
"metrics": ["ctr"],
"dimensions": ["date", "banner"],
"filter": {
"date":{
"from":"2016-08-01",
"to":"2016-08-30"
},
"campaign":{
"id":campaign_id
}
},
"paging":{
"page":1,
"pageSize":10000
}
}
Response is:
{
"reportData": {
"columnHeaders": [
"date",
"banner",
"ctr"
],
"columns": [
{
"key": "date"
},
{
"key": "banner"
},
{
"key": "ctr"
}
],
"rows": [
[
"2016-08-02T00:00:00",
"banner_1",
0.00816326530612245
],
[
"2016-08-03T00:00:00",
"banner_1",
0.0024213075060532689
],
[
"2016-08-03T00:00:00",
"banner_2",
0.001207653432082372
],
[
"2016-08-04T00:00:00",
"banner_1",
0.003472222222222222
],
[
"2016-08-04T00:00:00",
"banner_2",
0.000802886393241096
],
[
"2016-08-05T00:00:00",
"banner_1",
0
],
[
"2016-08-05T00:00:00",
"banner_2",
0.000676782107058102
],
[
"2016-08-06T00:00:00",
"banner_1",
0
],
[
"2016-08-06T00:00:00",
"banner_2",
0.000926995987708068
],
[
"2016-08-07T00:00:00",
"banner_1",
0.00904977375565611
],
[
"2016-08-07T00:00:00",
"banner_2",
0.0010050565441998231
],
[
"2016-08-08T00:00:00",
"banner_1",
0.0022935779816513758
],
[
"2016-08-08T00:00:00",
"banner_2",
0.000736000744868224
],
[
"2016-08-09T00:00:00",
"banner_1",
0.0052219321148825066
],
[
"2016-08-09T00:00:00",
"banner_2",
0.000636109173796044
],
[
"2016-08-10T00:00:00",
"banner_1",
0.0057971014492753624
],
[
"2016-08-10T00:00:00",
"banner_2",
0.000724849063441972
],
[
"2016-08-11T00:00:00",
"banner_1",
0
],
[
"2016-08-11T00:00:00",
"banner_2",
0.000581557986484298
],
[
"2016-08-12T00:00:00",
"banner_1",
0.0043103448275862068
],
[
"2016-08-12T00:00:00",
"banner_2",
0.000671168239505369
],
[
"2016-08-13T00:00:00",
"banner_1",
0
],
[
"2016-08-13T00:00:00",
"banner_2",
0.000803754549989838
],
[
"2016-08-14T00:00:00",
"banner_1",
0
],
[
"2016-08-14T00:00:00",
"banner_2",
0.000989294421086104
],
[
"2016-08-15T00:00:00",
"banner_1",
0.0064516129032258056
],
[
"2016-08-15T00:00:00",
"banner_2",
0.000638244734480941
],
[
"2016-08-16T00:00:00",
"banner_2",
0.000549180805298763
],
[
"2016-08-17T00:00:00",
"banner_2",
0.000551568224222697
],
[
"2016-08-18T00:00:00",
"banner_1",
0
],
[
"2016-08-18T00:00:00",
"banner_2",
0.000678091480705215
],
[
"2016-08-19T00:00:00",
"banner_2",
0
],
[
"2016-08-22T00:00:00",
"banner_2",
0.000360310246085577
],
[
"2016-08-23T00:00:00",
"banner_2",
0.000498299661680756
],
[
"2016-08-24T00:00:00",
"banner_2",
0.000561990345005873
],
[
"2016-08-25T00:00:00",
"banner_2",
0.000364882901395197
],
[
"2016-08-26T00:00:00",
"banner_2",
0.000372206184069575
],
[
"2016-08-27T00:00:00",
"banner_2",
0.000696784292763943
],
[
"2016-08-28T00:00:00",
"banner_2",
0.00084914217341799
],
[
"2016-08-29T00:00:00",
"banner_2",
0.000441720501352769
],
[
"2016-08-30T00:00:00",
"banner_2",
0.000556096204643403
]
]
}
}
So, total average CTR = 0.0015624820601283 = 0.15%
When I query without any dimensions:
{
"metrics": ["ctr"],
"filter": {
"date":{
"from":"2016-08-01",
"to":"2016-08-30"
},
"campaign":{
"id":campaign_id
}
},
"paging":{
"page":1,
"pageSize":10000
}
}
I get:
{
"reportData": {
"columnHeaders": [
"ctr"
],
"columns": [
{
"key": "ctr"
}
],
"rows": [
[
0.000729710843077063
]
]
}
}
CTR = 0.000729710843077063 = 0.07%
What is wrong?
Why such a difference: 0.15% vs 0.07%?
May be I should count CTR not by simple AVG but another way?
I also spotted the same issue with other APIs, such as Google Analytics API and Facebook Marketing API. Totals and averages counted locally using data sampled by dimensions are not always the same as totals provided by APIs themselves.
I'm just trying out some stuff with GeoJSON's by figuring out how to import them into MongoDB. Here is a snippet of the JSON data that I'm trying to import:
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"source": "© GeoBasis-DE / BKG 2013 (Daten verändert)",
"features": [
{
"type": "Feature",
"properties": {
"RS": "051580004004",
"DES": "Stadt",
"GEN": "Erkrath",
"EWZ_M": 20929,
"EWZ_W": 22883,
"SHAPE_AREA": 26441754.911268
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
6.88238674728364,
51.24166234476658
],
[
6.881975279589705,
51.23757802188462
],
[
6.891756626701045,
51.23929422878399
],
[
6.903180857084263,
51.23707789012778
],
[
6.908259412237354,
51.24206723420193
],
[
6.918977412277895,
51.24041847413137
],
[
6.926662904165135,
51.24253596694298
],
[
6.937590326994132,
51.23093391057761
],
[
6.94295724765998,
51.2299892278866
],
[
6.939417113978845,
51.227341409094315
],
[
6.951333593915336,
51.22740615125517
],
[
6.950253526637431,
51.224656373607054
],
[
6.955765647972802,
51.22167757046095
],
[
6.964606469495985,
51.22457614378809
],
[
6.975480633820623,
51.223046741435965
],
[
6.973404761769492,
51.22089502422256
],
[
6.980214287180364,
51.21998473062592
],
[
6.982741542191386,
51.214167748907066
],
[
6.994804384322499,
51.215655729126155
],
[
6.994747626413055,
51.21224925095131
],
[
6.985928005605313,
51.20743852466108
],
[
6.98828820437737,
51.20347146797952
],
[
6.944606700602787,
51.198908942197114
],
[
6.943253716046289,
51.19637267292547
],
[
6.937329237188316,
51.19846226521679
],
[
6.932905519738434,
51.194111787062006
],
[
6.907388585403191,
51.19594285093562
],
[
6.903803051706552,
51.20313804926765
],
[
6.910551758112292,
51.21099091708282
],
[
6.878932812936605,
51.21491884904021
],
[
6.872564112818471,
51.219486802570856
],
[
6.873244797535404,
51.226155851357795
],
[
6.878496898776498,
51.22869664243089
],
[
6.872981880136851,
51.232966792978665
],
[
6.871618163754948,
51.24209470504303
],
[
6.88238674728364,
51.24166234476658
]
]
]
}
},
{
"type": "Feature",
"properties": {
"RS": "051160000000",
"DES": "Stadt",
"GEN": "Mönchengladbach",
"EWZ_M": 123662,
"EWZ_W": 131172,
"SHAPE_AREA": 170540962.920759
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
.....
.....
I want now to import this data into my MongoDB instance. When I tried the following, I ran into some errors:
mongoimport --db /Users/joe/mongodb-data -c points --file "gemeinden_simplify200.geojson" --jsonArray
connected to: 127.0.0.1
assertion: 13106 nextSafe(): { $err: "Invalid ns [/Users/joe/mongodb-data.points]", code: 16256 }
I think you need to try-
mongoimport --db test -c points --file "gemeinden_simplify200.geojson" --jsonArray
Here test is database name.
I'm having the following model:
class
include Mongoid::Document
field :polygons, type: Hash
index({"polygon" => "2dsphere"})
end
When i try to insert the following document I get an error:
{
_id: ObjectId('53467c7f476f6c551c020000'),
polygons: {
type: "MultiPolygon",
coordinates: [ [ [ [ 13.00695419311523, 47.81822655820738 ], [ 13.03579330444336, 47.75825258545904 ], [ 13.09175491333008, 47.7658685011539 ], [ 13.07767868041992, 47.81707386519431 ] ], [ [ 13.00695419311523, 47.81822655820738 ], [ 13.03579330444336, 47.75825258545904 ], [ 13.09175491333008, 47.7658685011539 ], [ 13.07767868041992, 47.81707386519431 ] ] ], [ [ [ 13.07355880737305, 47.8260641920274 ], [ 13.05810928344727, 47.80323955290061 ], [ 13.10857772827148, 47.80116408820393 ], [ 13.1041145324707, 47.82514217887775 ], [ 13.08626174926758, 47.8290606216547 ] ], [ [ 13.07355880737305, 47.8260641920274 ], [ 13.05810928344727, 47.80323955290061 ], [ 13.10857772827148, 47.80116408820393 ], [ 13.1041145324707, 47.82514217887775 ], [ 13.08626174926758, 47.8290606216547 ] ] ] ]
}
}
The error message only says:
The operation: #<Moped::Protocol::Command ...> failed with error 16755: "Can't extract geo keys from object, malformed geometry?
According to http://geojsonlint.com/ the above geojson is valid. What am I doing wrong?
From what I see, your multipolygon rings are not closed. You need to actively end each ring with the same coordinate where it started. In your case, this would be valid geoJson Multipolygon.
{
type: "MultiPolygon",
coordinates: [
[
[
[ 13.00695419311523, 47.81822655820738 ],
[ 13.03579330444336, 47.75825258545904 ],
[ 13.09175491333008, 47.7658685011539 ],
[ 13.07767868041992, 47.81707386519431 ],
[ 13.00695419311523, 47.81822655820738 ]
],
[
[ 13.00695419311523, 47.81822655820738 ],
[ 13.03579330444336, 47.75825258545904 ],
[ 13.09175491333008, 47.7658685011539 ],
[ 13.07767868041992, 47.81707386519431 ] ,
[ 13.00695419311523, 47.81822655820738 ]
]
],
[
[
[ 13.07355880737305, 47.8260641920274 ],
[ 13.05810928344727, 47.80323955290061 ],
[ 13.10857772827148, 47.80116408820393 ],
[ 13.1041145324707, 47.82514217887775 ],
[ 13.08626174926758, 47.8290606216547 ],
[ 13.07355880737305, 47.8260641920274 ]
],
[
[ 13.07355880737305, 47.8260641920274 ],
[ 13.05810928344727, 47.80323955290061 ],
[ 13.10857772827148, 47.80116408820393 ],
[ 13.1041145324707, 47.82514217887775 ],
[ 13.08626174926758, 47.8290606216547 ],
[ 13.07355880737305, 47.8260641920274 ]
]
]
]
}
As per geojsonlint, I believe the syntax is valid, for example for multilinestring, but the linter would not enforce particular geometry type rules.
Please note however, that your MultiPolygon consists of two polygons, and each of them of two rings, but the inner ring has the same coordinates than the outer ring. While this might render in most geojson parsers, it is not topologically correct. Inner rings should intersect outer rings in a finite quantity of points. Having one or more edges equal means they intersect in infinite points.
This is the geometry without the inner rings
type: "MultiPolygon",
coordinates: [
[
[
[ 13.00695419311523, 47.81822655820738 ],
[ 13.03579330444336, 47.75825258545904 ],
[ 13.09175491333008, 47.7658685011539 ],
[ 13.07767868041992, 47.81707386519431 ],
[ 13.00695419311523, 47.81822655820738 ]
]
],
[
[
[ 13.07355880737305, 47.8260641920274 ],
[ 13.05810928344727, 47.80323955290061 ],
[ 13.10857772827148, 47.80116408820393 ],
[ 13.1041145324707, 47.82514217887775 ],
[ 13.08626174926758, 47.8290606216547 ],
[ 13.07355880737305, 47.8260641920274 ]
]
]
]
}
In second place, even if you remove the inner rings, you'd still have two overlapping polygons, which are not a valid multipolygon. You might be able to draw it, but somewhere down the line you might not be able to parse geometrical queries with that.
I have events in a collection, each containing a desired location, set a GeoJSON Polygon.
I also have service providers in another collection, also with a GeoJSON Polygon, indicating the area where they can deliver.
For a given service provider, I'm trying to list all the events that are in a compatible area.
However, I get this error:
Malformed geo query: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [ [ [ -31.59327575763251, 115.8574693000001 ], [ -31.59676306691357, 115.9162469300458 ], [ -31.60715789289806, 115.9738935747774 ], ...
My Polygons are closed (first and last coords are identical), so that's not coming from there.
Here are the events:
Events:
[
{
"_id": "5237d4bd9899e67c0e000004",
"area": "area",
"authtoken": "e3bf38ff9a1f132a7cc86ea045cf3951",
"budget": 1800,
"creation_date": {
"sec": 1379390653,
"usec": 0
},
"description": "Banana banana banana banana banana banana banana banana bananaaaaaaaaaaaaaaaaa!!!",
"ends": {
"sec": 1383343200,
"usec": 0
},
"geo": {
"type": "Polygon",
"coordinates": [
[
[
-33.740154878816,
150.9241267
],
[
-33.741901642057,
150.95422987078
],
[
-33.747108144055,
150.98375074498
],
[
-33.755673663135,
151.01211808166
],
[
-33.767432456928,
151.03878255704
],
[
-33.782156913661,
151.0632272399
],
[
-33.799561885809,
151.08497749366
],
[
-33.819310129033,
151.10361012448
],
[
-33.841018749004,
151.11876160464
],
[
-33.819310129033,
150.74464327552
],
[
-33.799561885809,
150.76327590634
],
[
-33.782156913661,
150.7850261601
],
[
-33.767432456928,
150.80947084296
],
[
-33.755673663135,
150.83613531834
],
[
-33.747108144055,
150.86450265502
],
[
-33.741901642057,
150.89402352922
],
[
-33.740154878816,
150.9241267
]
]
]
},
"guests": 180,
"loc": {
"city": "Liverpool",
"state": "NSW",
"country": "AU",
"lat": -33.9200192,
"lng": 150.9241267,
"timezone": "Australia\/Sydney"
},
"name": "Bananaaaaaaaa!",
"services": [
{
"creation_date": {
"sec": 1379390661,
"usec": 0
},
"data": {
"name": "Bananaaaaaaaaaaa!!!!!",
"request": "Banana!",
"description": " banana banana banana banana banana banana banana banana banana",
"budget": "1400"
},
"event": "aaca6751-75b2-4cd9-a76a-99d4ee56fb6a",
"service": "a9fe4bd0-d4f2-4f2c-8b81-4899ac19c44f",
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "15ceedd5-608b-48e4-8a05-b60ee4c4c5bb",
"type": "Food & Drinks"
}
],
"starts": {
"sec": 1383300000,
"usec": 0
},
"type": {
"_id": "521666701e6accf7afc1a0e3",
"icon": "uploads\/event-types\/icon-3891a6f4-760f-43f2-ab3a-a51c61fd8d0d.png?rnd=0.6323562911552134",
"name": "Meeting",
"uuid": "3891a6f4-760f-43f2-ab3a-a51c61fd8d0d"
},
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "aaca6751-75b2-4cd9-a76a-99d4ee56fb6a",
"within": {
"km": 20,
"addr": "liverpool, nsw"
}
},
{
"_id": "5237d50a9899e67c0e000005",
"area": "area",
"authtoken": "e3bf38ff9a1f132a7cc86ea045cf3951",
"budget": 1800,
"creation_date": {
"sec": 1379390730,
"usec": 0
},
"description": "Boo!!!",
"ends": {
"sec": 1383285600,
"usec": 0
},
"geo": {
"type": "Polygon",
"coordinates": [
[
[
-32.086853059112,
116.0081156
],
[
-32.088163946126,
116.03027505189
],
[
-32.092071203806,
116.05200507521
],
[
-32.098499107954,
116.07288445521
],
[
-32.1073230624,
116.09250825588
],
[
-32.118371984525,
116.11049558927
],
[
-32.131431584032,
116.12649694645
],
[
-32.146248475035,
116.14020095399
],
[
-32.330851613336,
116.10195418015
],
[
-32.289125251893,
115.86992020233
],
[
-32.2721989398,
115.86018847275
],
[
-32.254291845681,
115.85334073219
],
[
-32.235752983166,
115.84950870874
],
[
-32.098499107954,
115.94334674479
],
[
-32.092071203806,
115.96422612479
],
[
-32.088163946126,
115.98595614811
],
[
-32.086853059112,
116.0081156
]
]
]
},
"guests": 700,
"loc": {
"city": "Byford",
"state": "WA",
"country": "AU",
"zipcode": "6122",
"lat": -32.2217513,
"lng": 116.0081156,
"timezone": "Australia\/Perth"
},
"name": "Halloween",
"services": [
{
"creation_date": {
"sec": 1379390749,
"usec": 0
},
"data": {
"name": "Badass lighting",
"description": "Boom! I want that!",
"type": [
"Furniture",
"Lighting\/lasers",
"LED screens",
"Dancefloor"
],
"budget": "800"
},
"event": "a6c28e98-b647-4224-8bff-f0b7e5c453c3",
"service": "4d4f370e-5780-4794-b988-df9d7a9ed644",
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "217c4896-a986-4c0a-a6c7-46c9aa74cf27",
"type": "Equipment Hire"
}
],
"starts": {
"sec": 1383256800,
"usec": 0
},
"type": {
"_id": "521666701e6accf7afc1a0e3",
"icon": "uploads\/event-types\/icon-3891a6f4-760f-43f2-ab3a-a51c61fd8d0d.png?rnd=0.6323562911552134",
"name": "Meeting",
"uuid": "3891a6f4-760f-43f2-ab3a-a51c61fd8d0d"
},
"uid": "0d92f274-67a3-4d2f-b1c8-d771fd84c113",
"uuid": "a6c28e98-b647-4224-8bff-f0b7e5c453c3",
"within": {
"km": 15,
"addr": "byford"
}
}
]
And here is my query object:
db.events.find({
"geo": {
"$geoIntersects": {
"$geometry": {
"type": "Polygon",
"coordinates": [
[
[
-31.593275757633,
115.8574693
],
[
-31.596763066914,
115.91624693005
],
[
-32.087153956437,
116.25114123685
],
[
-32.13233852915,
116.22535569049
],
[
-32.174036124673,
116.19236795072
],
[
-32.304841443191,
115.76898154226
],
[
-32.290955790481,
115.7119268346
],
[
-31.841206471153,
115.45474133646
],
[
-31.794742552646,
115.4770638401
],
[
-31.751364552714,
115.50675244396
],
[
-31.711911850434,
115.5432228167
],
[
-31.677146863424,
115.58576206658
],
[
-31.607157892898,
115.74104502522
],
[
-31.596763066914,
115.79869166995
],
[
-31.593275757633,
115.8574693
]
]
]
}
}
}
})
What am I missing?
My query seems to be identical to the official mongodb doc for geo queries.
Thanks in advance!
Your coordinates are specified as [latitude, longitude]. MongoDB requires that you specify coordinates in the opposite order for 2dsphere geometry: [longitude, latitude].
Your query worked for me once I swapped the coordinate order.