Polygon contains polygon [MONGODB] - node.js

My issue is the following. I have a collection (territories) that have a geometry of type Polygon inside. The thing is that the polygons in my applications must be validated against this collection. Meaning that for a polygon to be valid, it must be COMPLETELY contained in at least one territory.
As $geoWithin is intented to work only in one direction. I try to do the following.
db.territories.insertMany([
{
_id: 1,
geometry: {
type: "Polygon",
coordinates: [
[
[0, 0],
[5, 0],
[5, 5],
[0, 5],
[0, 0]
]
]
}
}
])
const aggregation = [
{
$addFields: {
polygonToValidate: {
type: "Polygon",
coordinates: [[[2, 2], [4, 2], [4, 4], [2, 4], [2, 2]]]
},
territoryPolygon: "$geometry",
},
},
{
$match: {
polygonToValidate: {
$geoWithin: {
$geometry: "$territoryPolygon"
}
}
}
}
]
db.territories.aggregate(aggregation);
The polygon is valid as it is completely contained inside the territory polygon. But is not posible to reference the document field, neither the field added in the $addFields (territoryPolygon).

Related

Is there a way to display the length of the sides of a jsxgraph polygon?

I'm trying to draw a polygon from a list of points with labels on each of the sides (borders) with the length of the sides?
Is there a simple way to get this happening?
Here's a snippet with what I've got at present. Note that I want to be able to use an arbitrary number of points, not just three if at all possible.
const board = JXG.JSXGraph.initBoard('jxgbox', {
boundingbox: [-5, 5, 5, -5],
axis: true
});
var points = [
[0, 0],
[0, 1],
[1, 0]
];
var pg = board.create('polygon', points, {
fixed: true,
hasInnerPoints: true,
vertices: {
visible: false
},
borders: {
names: ['a', 'b', 'c'],
withLabel: true
}
});
It is possible to set the label text of each border to be a function. This has to be done after the construction of the polygon. A possible solution would look like this:
const board = JXG.JSXGraph.initBoard('jxgbox', {
boundingbox: [-5, 5, 5, -5], axis:true
});
var points = [
[0, 0],
[0, 2],
[2, 0]
];
var pg = board.create('polygon', points, {
fixed: true,
hasInnerPoints: true,
vertices: {
visible: false
},
borders: {
names: ['a', 'b', 'c'],
label: { offset: [-10, 10] },
withLabel: true
}
});
// Overwrite the labels of the borders:
for (let i = 0; i < pg.borders.length; i++) {
pg.borders[i].label.setText( () => pg.borders[i].L().toFixed(2) );
}
See https://jsfiddle.net/h6knmgjc/ for a working example.

Comparing the values of common keys in multiple Nested Dictionaries

Below is the Input_dict for data analysis:
input_dict =
{
"C:\\arm64\\lib_apple.so": { "func-abc": [5,6,7,8], "func-123":[1,1,1,1] },
"C:\\arm64\\lib_banana.so": { "func-123": [2,3,4], "func-rt": [0,0] },
"C:\\armeabi\\lib_banana.so": { "func-123": [1,0,0], "func-rt": [1,5] },
"C:\\armeabi\\lib2.so": { "func-0": [1]},
"C:\\x86\\lib_apple.so": { "func-so": [5,6,7,8], "func-123": [2,2,1,1] },
"C:\\x86\\lib_banana.so": { "func-rt": [2,0] },
"C:\\x86\\lib2.so": { "func-0": [1,2,3]}
}
The aim is to compare the 'values' of functions with same name of different architectures(arm64,armeabi,x86).
In other words, I want to compare the "lists" of functions(with same name) in different libraries(.so) files.
For example: Comparing func-123: [2,3,4] with func-123: [1,0,0] from arm64\lib_banana.so and armeabi\lib_banana.so respectively.
One of the desired output could be:
{ lib_apple.so: { func-123: [arm64,[1,1,1,1]],[x86,[2,2,1,1]]}}
You can restructure your function data to order by their name first, then supported architectures. Afterwards, print out those functions that appear in multiple architectures:
from collections import defaultdict
from pathlib import PureWindowsPath
lib2func = {
r'C:\arm64\lib_apple.so': { 'func-abc': [5,6,7,8], 'func-123': [1,1,1,1] },
r'C:\arm64\lib_banana.so': { 'func-123': [2,3,4], 'func-rt': [0,0] },
r'C:\armeabi\lib_banana.so': { 'func-123': [1,0,0], 'func-rt': [1,5] },
r'C:\armeabi\lib.so': {},
r'C:\armeabi\lib2.so': { 'func-0': [1]},
r'C:\x86\lib_apple.so': { 'func-so': [5,6,7,8], 'func-123': [2,2,1,1] },
r'C:\x86\lib_banana.so': { 'func-rt': [2,0] },
r'C:\x86\lib2.so': { 'func-0': [1,2,3] },
}
# restructure
func2arch = defaultdict(dict)
for lib_path, functions in lib2func.items():
path = PureWindowsPath(lib_path)
lib = path.name
arch = path.parent.name
for func_name, func_val in functions.items():
func2arch[(lib, func_name)][arch] = func_val
# find functions defined for multiple architectures
for (lib, func_name), arch_data in func2arch.items():
if len(arch_data) < 2:
continue # skip functions that only appear once
print(lib, func_name, arch_data)
gives
lib_apple.so func-123 {'arm64': [1, 1, 1, 1], 'x86': [2, 2, 1, 1]}
lib_banana.so func-123 {'arm64': [2, 3, 4], 'armeabi': [1, 0, 0]}
lib_banana.so func-rt {'arm64': [0, 0], 'armeabi': [1, 5], 'x86': [2, 0]}
lib2.so func-0 {'armeabi': [1], 'x86': [1, 2, 3]}
The above code assumes that library/function name pairs are unique.

Spatial Indexes with DocumentDB

I’m trying to do a spatial query against DocumentDB that looks like this:
SELECT * FROM root r WHERE
ST_WITHIN({'type':'Point','coordinates':[-122.02625, 37.4718]}, r.boundingBox)
to match a document that looks like this in the collection:
{
"userId": "747941cfb829",
"id": "747941cfb829_1453640096710",
"boundingBox": {
"type": "Polygon",
"coordinates": [
[-122.0263, 37.9718],
[-122.0262, 37.9718],
[-122.0262, 36.9718],
[-122.0263, 36.9718],
[-122.0263, 37.9718]
]
},
"distance": 0,
"duration": 1
}
I’ve turned on spatial indexes ala https://azure.microsoft.com/en-us/documentation/articles/documentdb-geospatial/ but I’m not getting a match back from DocumentDB.
Any ideas?
NOTE: Corrected GeoJson coordinate order.
The correct specification of a GeoJSON polygon has an additional array around the coordinates than you show to allow for the possibility of holes and multipolygons. So, it would look like this:
{
"type": "Polygon",
"coordinates": [
[
[0, 0], [10, 10], [10, 0], [0, 0]
]
]
}

changing the x-axis to string datatype using ticks doesn't work

Below is my script. Even after using ticks in option the x-axis doesn't change to the specified string. Also how to set from which number y-axis start? Currently it starts at 0.0 but I need it to start at 0.1
var data = [[1, 0.1], [2, 0.2], [3, 0.5], [4, 0.8], [5, 0.8], [6, 0.8], [7, 0.8], [8, 0.9], [9, 1.0], [10, 1.0], [11, 1.0], [12, 1.0]];
var dataset = [{label: "unit price",data: data}];
var options = {
series: {
lines: { show: true },
points: {
radius: 3,
show: true
},
xaxis:{ticks: [[1,"foo"], [2,"bar"], [4,"cat"], [5,"woo"], [7,"cookie"], [8,"yay"] ]}
}
};
$(document).ready(function () {
$.plot($("#flot-placeholder"), dataset, options);
});
The xaxis property belongs directly in the top level of the options not under the series property.
And for the minimum of an axis you can use the min property.
var options = {
series: {
lines: {
show: true
},
points: {
radius: 3,
show: true
}
},
xaxis: {
ticks: [
[1, "foo"],
[2, "bar"],
[4, "cat"],
[5, "woo"],
[7, "cookie"],
[8, "yay"]
]
},
yaxis:{
min: 0.1
}
};
See this fiddle for the full example and the documentation for more info.

MongoDB update query for nest array

Having collection Measurement such as shown below:
{
"Data" : [ [-5, [[1, 1023.0], [2, 694.0]]], [-1, [[1, 0.0], [2, 20.0]]], [-3, [[1, 30.75], [2, 30.75]]] ]
}
it reflects c# structure of Dictionary<int, Dictionary<int, double>> - what I'd need to do is to write an update script which will add 5 to all the parental dictionary keys. How could this be done via mongo update script? So it would turn the object to look as follows:
{
"Data" : [ [0, [[1, 1023.0], [2, 694.0]]], [4, [[1, 0.0], [2, 20.0]]], [2, [[1, 30.75], [2, 30.75]]] ]
}
The only way to do this is programatically, i.e., looping over the Data array and updating each individually.
This is probably not the structure that you really want if you need to update things in this way. The problem lies with the ability to match elements in a nested array in that the current limitation is that you can only match the first position and reference that index only when doing an update.
We can't tell much about your purpose based on what you have presented, but what you probably need is something like this:
{
"Data" : [
{
"pos": 0,
"ref": -5,
"A": { "x": 1, "y": 1023.0 },
"B": { "x": 2, "y": 694.0 }
},
{
"pos": 1,
"ref": -1,
"A": { "x": 1, "y": 0.0},
"B": { "x": 2, "y": 20.0 }
},
{
"pos": 2,
"ref": -3,
"A": { "x": 1, "y": 30.75 },
"B": { "x": 2, "y": 30.75 }
}
]
}
Yet even that does not allow you to update in a single query. You can do it with one for each element though:
db.collection.update({"_id": id, "Data.pos": 0}, {"$inc":{"Data.$.ref": 5}});
db.collection.update({"_id": id, "Data.pos": 1}, {"$inc":{"Data.$.ref": 5}});
db.collection.update({"_id": id, "Data.pos": 3}, {"$inc":{"Data.$.ref": 5}});
And your current schema would not allow you to do even that. And at least all of the elements could be accessed in this way, which again they could not before.
In any case, updating all of the array elements at once is not possible other than in a loop:
db.collection.find({ "_id": id }).forEach(function(doc) {
doc.Data.forEach(function(data) {
data.ref += 5;
});
db.collection.update(
{ "_id": doc._id },
{ "$set": { "Data": doc.Data } }
);
})
Or some variant that might even do something like the first example rather that just replacing the whole array as this does. Your current structure would rely on looping through several nested arrays to do the same thing.
Of course if you regularly have to update all elements in this way, then consider something other than an array. Or live with how you have to update, according to what your data access needs are.
Read the documentation on how things can be handled and make you decisions from there.

Resources