Apache ignite geo spacial queries examples - geolocation

I am looking for an example using Apache Ignite where we have a bunch of geo points like example locations in a city a we do a query for points close to a certain point within a radius.
I have found only an example of POLYGON search at:
https://dzone.com/articles/geospatial-queries-with-apachereg-ignite
Thank you, kindly
Luis Oscar

Ignite uses Java Topology Suite to support spatial queries. As far as I know, circular geometry is not supported by JTS.
But you can still approximate a circle with a polygon. If you need exactly the points, that fit into a circle, you can query for points, that lay in a circumscribed regular polygon and after that filter out the points, that have greater distance, than the specified radius. The filtering may be performed in code outside the query.

As said above, you could use a "greater rectangle or square" which is the smaller rectangle containing your circle. Then loop through the results with the isWithinDistance(...) method from your orignal point to filter the point inside the rectangle but not inside your circle.
However as ignite uses JTS, which is a planar representation, according to my tests, you can't make accurate search with spherical(or ellipsoid) projection (like WGS84) and many results would be wrong. Most likely the Polygon search is not accurate and the isWithinDistance won't filter correctly, unless you are searching something at less than 10meters from the original point.

Related

Translating Lat/Lon coordinates to X,Y plane, with control points

I have an arbitrary map image, which may or may not be accurately projected to some standard geographic mapping. Probably not, though, since it's an artists rendition. Consider this map a 2D image of pixels at 0,0 onward.
I'd like to map lat/lon points in world space to this map. Since the map is not necessarily a known or accurate projection, I've got to come up with some other solution. I figure that establishing control points on the 2D image that correlate to known lat/lon values is step #1. At a minimum, 3, but maybe more, in case it's required to sort out distortion in the map image.
What algorithm or equation would I be looking for to take these control points, and identify the X,Y position on the image from any given lat/lon input?
I expect it to be inaccurate, depending on the number of control points. And I expect, for some weirder images, to have to go and add many control points in certain areas to make it line up right.
When the area depicted is small, (e.g. it fits in a square few km on the side), one thing to try is described below. I'm sorry if you find the description too terse, I wanted to keep it reasonably short.
The idea is to assume the image is in some unknown conformal projection, and to try to approximate it. Of course this may fail, if the image can not, in fact, be reasonably approximated this way.
Given your control points P[], project them into map coordinates Q[] using some conformal projection, and get hold of their image coordinates R[]. To within a metre or so -- given the assumption above -- the R[] can be obtained from the Q[] by a transformation T that is a translation, an (isotropic) scaling and a rotation. You can then find T, say by least squares, using the Q[] and R[]. You have a two stage map from the control point geographic coordinates P[] to their image coordinates R[]: first project using the chosen projection, then apply T. You could use the inverse of this map to go from arbitrary image coordinates to geographical coordinates.
If the image is larger than a few km, you may not get enough accuracy this way. All is not lost. Though a translation, scale and rotation may not suffice, any two conformal projections are related by a (complex) analytic map. So you could try to fit (an approximation to) this map using the control points as above. A suitable approximation might be a complex polynomial, or a complex rational function.
If I were doing this, I think I would first test it on artificial data. For example you could generate images of various sizes, using some projection (differing from the one used above), and see how well you fit the known points in these images.

Polyline drawMapRect optimized drawing

I have lots of long polylines on a map.
I'd like to optimize their drawing, because at a few thousand points the polylines are drawn incredibly slow.
My drawMapRect looks like this :
- for each polyline segment
- verify if it's bounding box intersects the currently drawn MKMapRect
- if id does, draw it
Which does great if there aren't too many points. But when there are 8-16 maprects visible and 2-3000 points, they are incredibly slow running throught the for.
If they would be only locations, a solution would be to implement some kind of quadtree/r-tree structure and only filter for those locations in the currently drawn MKMapRect, but I'm not sure about if that would be appropiate for the polylines themselves.
If I filter only for the segment endpoints inside the current maprect, then some line segments might not be drawn. For example, the two red maprects between points 1-2 have no segment endpoints in them but still need to draw ...
Is there some kind of algorithm similar to quadtrees or some kind of approach for this problem ?
Unfortunately, I don’t know such a data structure that would allow to check line intersection with a rectangle.
However, one approach to solve the problem might be the following:
Draw all polylines in a map of very low resolution (a 2 dim array), and note for every pixel which polyline has drawn it. Then scan in this low-res map the relevant rectangles for drawn pixels, and store all relevant polylines. These can then be drawn in the full resolution map.
Maybe this approximate algorithm is faster than the precise algorithm that you use right now.
EDIT:
I assume you use for the polyline MKMapRect intersection check an effective algorithm such as shown in How to find the intersection point between a line and a rectangle?.

Detect if polygon has intersecting lines (bowtie)

I'm looking for a way to detect if a set of points/coordinates have any intersecting lines.
A little setup, I'm drawing a polygon using UIBezierPath on an overlay to a map. This all works. I'm able to reduce the map points down using a point reducing algorithm, and I'm left with a simple looking polygon that renders on my map just fine. FWIW, I'm using Google Maps SDK.
My problem is that it is possible for the user to draw a polygon with self intersecting lines (which is a problem for what I am doing). I need to be able to do one of 3 things.
Remove the intersecting points in the array. (Clip off the bow tie pieces)
Detect if my points have this bow tie (I'll just tell them to redraw a new polygon)
If possible (which I don't think it is), prevent the path from drawing the bow tie in the first place.
I mostly see the bow tie when the polygon self closes and the end point is slightly underlapping the start point. So when the polygon closes and renders into map coordinates on the map, I get a tiny bow tie that messes with an internal API.
Is there anything out there that will work using map coordinates? I've seen some fixes for regular CGPoints, but nothing that will take map coordinates. I would prefer to do this check on my polygon after it has gone through my reducer as it leaves many less points to check. Performance is an issue, and would prefer not to iterate over hundreds of points directly coming off the UIBezierPath. Any help would be appreciated.
I don't know about the Google Maps SDK or the UIBezierPath. I assume that you are given a polygon in the 2D plane and you would like to automatically detect where the polygon intersects itself (if it does).
Perhaps the easiest way to do this is checking all pairs of edges whether they intersect or not. You can check this in O(n2) time where n is the number of edges, as there are n*(n-1)/2 pairs of edges. For a given pair of edges, here are the details how to do it:
How to check if two given line segments intersect?
Nothing extraordinary but the details do require attention.
A more sophisticated algorithm is the plane sweep algorithm:
Line segment intersection, starting at slide 25
Line Segment Intersection Using a Sweep Line Algorithm

Best way to search a point across several polygons

I need to match a given point (lat, lon) against several polygons to decide if there is a match.
The easiest way would be to iterate over each polygon and apply the point-in-polygon check algorithm, but that is prohibitively expensive.
The next optimization that I did was to define a bounding rectangle for each polygon (upper bound, lower bound) and iteratively check the point against each bounding box (fewer comparisons as against checking all the points in the polygon).
Is there any other optimizations possible? Would a spatial index on the bound rectangle points or a geohash help?
Further optimizations:
The bounding box idea is good. Checking if a point is in a bounding box is extremely fast.
If you still need more speed, you can do more pre-calculation like this:
For each polgon create a bounding box.
Define equally sized "tiles" that cover your map.
For each tile, create a list of polygons that overlap. You can do that by first checking if the bounding box overlaps with the tile. If they do, you check if the polygon overlaps with the tile.
When searching, do this:
Determine the tile that you're in. That's a fast operation.
Now you have the list of potential polygons.
For each polygon, check if the point is in the bounding box.
if it is, check if the point is in the polygon using the more expensive algorithm that you've mentioned.
I've used this algorithm several times and it's very fast. By changing the tile size you can choose the right balance between memory footprint and performance:
Think of the extreme cases:
One huge tile that covers the entire map:
You'll get one list of all elements in your map, you'll have to check all of the bounding boxes.
Very tiny tiles (1x1 m for a map that only has a polygon per country):
You'll get a huge amount of tiles. All polygons will be split over many tiles, and each tile will only have one polygon. But, once you've figured out in which tile the point is (fast), it's almost 100% sure that there's just one polygon that needs to be checked.
You need to be somewhere in between. If you only need this once and a while, you might want to choose a low memory footprint over performance. The optimal tilesize can also depends on the homogeneity of the polygon sizes. So, there is no automatic way to calculate an optimal tile-size, and you'll just have to tweak a bit until you get it right.

Will treating MKCoordinateRegion like a rectangle come back to haunt me?

Background: I'm build a series of location-based apps that make heavy use of maps. These maps are annotated with locations fetched from a server, to which I pass regions of the map I need data for (defined as a lat/long and a latDelta/longDelta, much like MKCoordinateRegion, but with a different location of the reference coordinate). I'm writing a bunch of helper methods/classes to use when managing these regions. Compatibility with iOS 3.x is required (meaning MKMapRect is out).
Question: Am I setting myself up for failure by treating MKCoordinateRegions like rectangles? Specifically, I'm treating their geometry as if it was that of a rectangle, assuming they have basically the same properties as rectangles. I've implemented several methods that mirror CGRect's helper methods, such as MKCoordinateRegionUnion/Inset/Outset, etc, and they all pass my unit tests, but I'm starting to question if my underlying assumptions are correct. I know in fact that MKCoordinateRegion does not represent a geometric rectangle, but rather a region of a spherical surface bound by two sets of parallel planes, perpendicular to each other (bonus points if somebody can clue me in on a better term for that).
I'm not experiencing any anomalies yet, but since many apps will be reliant on my understanding of the geometry, I'd rather figure out now if I'm going down the wrong path. The fact that I slept through most of the classes in school dealing with 3d radial geometry doesn't give me much confidence that my intuition is correct.
If you are taking into account the equator, prime meridian and dateline edge cases, I think you will be ok.
Alternatively, you could develop your own MKMapRect like rects. Troy Brant has a great blog post about the how the rects are formed:
http://troybrant.net/blog/2010/01/mkmapview-and-zoom-levels-a-visual-guide/
While the blog post is mainly about zoom levels, all the information there can be used to build up your own map rect library.
As for areas bounded by great circles on the surface of a sphere, they are called spherical polygons. So I guess you could just call them spherical rectangles.

Resources