Calculation for extending a geo bounding box by x miles - elasticsearch

Hoping someone can help me out with a calculation.
Say I have a coordinates for a bounding box, for example as a viewport returned from Google Places API, e.g.
"viewport" : {
"northeast" : {
"lat" : 37.4238253802915,
"lng" : -122.0829009197085
},
"southwest" : {
"lat" : 37.4211274197085,
"lng" : -122.0855988802915
}
}
I'm then using these to return results from Elasticsearch filtering by locations within a Geo Bounding Box: https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-geo-bounding-box-query.html
This is fine, however i'd like run a calculation to increase this viewport by, for example, 5 miles with the return value being another bounding box of increased size.
I guess the diagonal line running through the viewport would need to be increased by 2.5 miles each way, but I'm not sure on the calculation.
Thanks in advance.

So it turns out this can be solved using Vicenty's formulae: (https://en.wikipedia.org/wiki/Vincenty%27s_formulae#Direct_Problem).
JavaScript implementation at https://www.npmjs.com/package/geodesy for anyone interested.
Praise Vicenty.

Related

Mapping a sphere with a grid of Polygons

I'm currently trying to map a sphere with squares using d3.js and GeoJon. I've found a script which generate a grid using MultiPolygon : https://jsfiddle.net/94dhdmnw/
{
type: 'FeatureCollection',
features: [{
type: 'Feature',
geometry: {
type: 'MultiPolygon',
coordinates: [coordinates]
}
}]
}
Nonetheless, I need an id for each square of my grid. That's why I want to map my sphere using Polygons instead of MultiPolygons. Thus, I've adapted the script in order to get a Polygon mapping : https://jsfiddle.net/et3s6p33/
Yet, when I map my sphere using the file obtained through the Polygon script, I have only half of my sphere which is mapped - not even with the right squares' size.
Sphere with MultiPolygon mapping :
Sphere with Polygon mapping :
Any ideas why?
I am using this script to map my sphere : http://jsfiddle.net/6n230b3w
Edit : Or maybe there is a way to set a property for each square of my MultiPolygon?

Algorithm keystroke dynamics

I have a third party keyboard app who's algorithm I'm trying to understand from a key map data file I have. It monitors the way you tap on your keyboard and then it adjusts the tappable area behind the scenes so as to accept user input more accurately. I'm now trying to similarly mimick this keyboard concept for a small game I am making.
This is the tappable character map the keyboard app rendered:
Here's an example of a character map for the keyboard letter V
{
"v" :
{
"characters" : [ "v" ],
"feature-threshold-multiplier" : 1.0,
"initial-scale-multiplier" : 1.0,
"mean" :
{
"dof" : 40,
"mode" : [ 210.0367889404297, 138.3223266601562 ]
},
"precision" :
{
"dof" : 40,
"mode" :
[
0.003064915072172880,
-0.0009184600203298032,
-0.0009184600203298032,
0.006329041905701160
]
},
"prior-mean" : [ 207.0, 142.50 ],
"prior-precision" : [ 0.004667554982006550, 0.0, 0.0, 0.004667554982006550 ]
}
}
Question
What I'm trying to figure out now is which part of the data set determines the size of the tappable area and which part determines the rotation.
My findings
Some things I have noticed which may help others in helping me understand what the keys and their values actually relate to:
I have found that precision -> mode contains two values which are always the same for every single character map.
I have noticed that there is a dof key for both the mean and
mode array, and they are always the same.
So far a friend has been able to figure out what one of the key values mean which contribute to the positioning of each tappable key.
The mean -> modecontains the x and y position.
This link shows a code output rendering of the character dataset given.
http://codepen.io/martinlindhe/pen/yebpgO
You'll notice that the output matches the exact positioning of the tappable key map shown below.
I can only guess here but my bet is:
pos = 210.0367889404297, 138.3223266601562
is position (corner or center) and
0.0030649150721728800,-0.0009184600203298032
-0.0009184600203298032, 0.0063290419057011600
are 2 x 2D basis vectors (so size and orientation of your tapping area) and size = 40 (which more or less matches position). I see 2 possible layouts (row-major or column-major) but your data is symmetric so no way to tell which one it is as both are:
u = 40 * ( 0.0030649150721728800,-0.0009184600203298032)
v = 40 * (-0.0009184600203298032, 0.0063290419057011600)
As you did not provide any info about coordinate system and also the position does not match the image at all (you post most likely scaled image) I can not verify any of them. The only thing that is visible is u is ~2 times the size of v and as this is for a V key (I assume) then I see it like this (infered from all data you provided):
as you can see the u vector is mirrored (god knows why).
To verify you should check/compare more than just single key. To get the scaling I would chose W,Z,M,O as they form almost a rectangle covering almost whole keyboard... And also check differently rotated keys to verify the weird mirroring.

Process multiple polygons d3

The answer below on drawing a polygon works well for a single polygon. However, what if there are more than one polygon? Simply adding an additional polygon with points seems not to work even though using "select all" would seem to indicate that it would be OK to add a couple more polygons without much problem..
We have an array of polygons, each of which has an attribute Points which is an array of points.
The first array with polygon should obviously be mapped and the point arrays of each member processed as described. But how to spedify this two-level structure with d3?
Proper format for drawing polygon data in D3
The answer is simple and straightaway. Just pass the array of polygons as data to d3 selection.
In your case it seems that you are using an array of polygons which are composite objects, each having a key called 'Points'. I assume it looks something like this-
var arrayOfPolygons = [{
"name": "polygon 1",
"points":[
{"x":0.0, "y":25.0},
{"x":8.5,"y":23.4},
{"x":13.0,"y":21.0},
{"x":19.0,"y":15.5}
]
},
{
"name": "polygon 2",
"points":[
{"x":0.0, "y":50.0},
{"x":15.5,"y":23.4},
{"x":18.0,"y":30.0},
{"x":20.0,"y":16.5}
]
},
... etc.
];
You will just have to use d.Points instead of d when writing the equivalent map function, which can be written as follows-
vis.selectAll("polygon")
.data(arrayOfPolygons)
.enter().append("polygon")
.attr("points",function(d) {
return d.points.map(function(d) {
return [scaleX(d.x),scale(d.y)].join(",");
}).join(" ");
})
.attr("stroke","black")
.attr("stroke-width",2);
You can check the following working JSFiddle to verify.
EDIT- The same example as above with convex hull implementation for rendering complete polygons. http://jsfiddle.net/arunkjn/EpBCH/1/ Note the difference in polygon#4

Pointlabels not displaying when data point is at maximum

I have the following graph: http://synicworld.com/media/graph.png
Is there a way to get the pointlabel to show on the bars that have maximum value?
pointLabels {
edgeTolerance: 100
}
Looks like it's just edgeTolerance, which I had tried before, but not with a value high enough.
pointLabels {
edgeTolerance: -20
}
This will allow rendering of point labels even if they're too close to the edge. The negative value means there might be overlap onto the chart.

Wrong distance returned by ElasticSearch distanceInKm

I've implemented a script field that returns distance like here Return distance in elasticsearch results?
Ruby:
{
script_fields: {
distance: {
script: "doc['#{source_field}'].distanceInKm(#{lat}, #{lng})"
}
}
}
However the distance returned isn't correct. If the point is close, the distance is more correct but as it gets further away it gets more and more incorrect.
Can't seem to figure out what I'm doing wrong.
distanceInKm() calculates distance as points on a plane, which is fast but not very accurate. Try using arcDistanceInKm() instead.

Resources