Mongodb GeoSpatial querying within shapes in collection - mongodb

I have a collection that has a 2d geospatial index on a field (center) which is an array of long/lat, the collection also has a radius field. So each item can represent a circle. I know that mongodb has a operator $within, and I want to get a list of all items that contain a specific point [long,lat], but it seems that I can only check which points are within a specific shape.

You are correct, right now, you can't do what you want. Please file a feature request at http://jira.mongodb.org as I can't find one already existing for this.

This is how I solved it (i.e. get a shape that covers a given point) in my situation, using a basic grid. It has a limited accuracy, depending on the grid resolution:
create a collection "grid" with points that covers the common bounding box of all your shapes (use a nested for loop in javascript)
create a 2d index on the grid
for each shape, search for all the grid points that lie within the shape; label each grid point with the id of the shape (use an array attribute on the grid point)
To see in what shape(s) a point lies, search for the closest grid point, and return its assigned shape(s) property. First check for the common bounding box, because points outside it should always return 'not in any shape', and not use the closest grid point.
Depending on the precision you need, this might or might not be a usable solution. The accuracy depends on how many points you put in your grid, and you may be able to do something smart with local densities of your grid.

Related

Matlab: Find pattern in an image given a skeletonized template

I am stuck at a current project:
I have an input picture showing the ground with some shapes on it. I have to find a specific shape with a given template.
I have to use distance transformation into skeletonization. My question now is: How can I compare two skeletons? As far as I noticed and have been told, the most methods from the Image Processing Toolbox to match templates don't work, since they are not scale-invariant and rotation invariant.
Also some skeletons are really showing the shapes, others are just one or two short lines, with which I couldn't identify the shapes, if I didn't know what they should be.
I've used edge detection, and region growing on the input so there are only interessting shapes left.
On the template I used distance transformation and skeletonization.
Really looking forward to some tips.
Greetings :)
You could look into convolutions?
Basically move your template over your image and see if there is a match, and where.
The max value of your array [x,y] is the location of your object in the image.
Matlab has a built-in 2D convolution function for this

Need help producing this code: Dynamic fieldnames to calculate distance btw centers of two shapes

So I have a structure that was developed based on user's input and preference. The code begins with the user choosing between 3 given shapes: circle triangle, and square. Each of these shapes is structures themselves, the CIRCLE struct has {'radius', random int, 'center, [random_int1, randomint2] and area,[]), the area is calculated only if the user chooses the shape, although it's probably not pertinent to what I am asking. Additionally, the other two shapes have FN's (sidelength, center, area). So basically the center of each is valued with a 1x2 double, both randomly assigned.
after the shapes are created and displayed to the user, the user chooses two of the shapes. These two shapes are to undergo DYNAMIC FIELD NAME computation to calculate the distance between the two centers using sqrt((x2-x1)^2 + (y2-y1)^2)). Following this, we need to calculate the ratio of areas of two shapes, which are again chosen by the user. for example, it states "if the user chooses square and circle, calculate the ratio of the areas of the square to the circle, using dynamic field names.
shape = struct('circle;, [], 'square',[], 'triangle', [])
I'm new to Matlab, so please if possible, explain accordingly. I'm so nervous about this class, that I'm literally shaking right now after being up for almost 2 days trying to learn all this. It's really intense.

How can to do shape math with bing maps?

If I have two shapes displayed on Bing-Maps can I do any set Math?
What I really want is to take two intersecting shapes, determine a 3rd shape for the intersection and then reduce the the original shapes to exclude the intersection.
I actually have the intersection shape so the key bit is taking a 'bite' out of the original shapes.
Reason for doing this is currently displaying the intersection as a third layer (to give a specific colour) produces a largely occluded map, what with part of the transparent shape1 and shape2 and the intersection all covering the same bit of the map.
If I could 'cut-down' the two main shapes only the intersection shape would colour/cover that part of the map.
I do not think there are out of the box solutions in the Bing Maps API. I have done something similar in the past with Bing Maps API and SqlGeography Class though. Its easy to loop through the points of our Bing Polygons, therefore we can plot them into SqlGeography Polygons or SqlGeometry Polygons.
You will need the Microsoft.SqlServer.Types.dll to make use of this class, but it is free with SqlExpress.(Note that you do not actually need SQL Server installed, you just need to reference the DLL)
You can use the SqlGeography Class (or SqlGeometry Class) to find intersecting shapes and points. Perhaps have a look at STIntersection Method, According to MSDN STIntersection will;
"Returns an object representing the points where a SqlGeography instance intersects another SqlGeography instance."
Once you have the object representing the shape were a SqlGeography instance intersects another, you know you need to somehow adjust any points from the compaired instances that lie withing the SqlGeography instnace returned by StIntersection.
Here is a simular question where Peter uses STDifference. To subtract the two shapes.
I'll provide you with a c# example I made quickly for the Intersection. I had a lot of trouble finding any code examples for anything that wasn't purely SQL Server based.
SqlGeography Shape1 = new SqlGeography();
SqlGeographyBuilder geographyBuilder1 = new SqlGeographyBuilder();
geographyBuilder1.SetSrid(4326);
geographyBuilder1.BeginGeography(OpenGisGeographyType.Polygon);
geographyBuilder1.BeginFigure(47.4275329011347, -86.8136038458706);
geographyBuilder1.AddLine(36.5102408627967, -86.9680936860962);
geographyBuilder1.AddLine(37.4928909385966, -80.2884061860962);
geographyBuilder1.AddLine(38.7375329179818, -75.7180936860962);
geographyBuilder1.AddLine(48.0932596736361, -83.7161405610962);
geographyBuilder1.AddLine(47.4275329011347, -86.8136038458706);// Remember last point in the polygon should match the first
geographyBuilder1.EndFigure();
geographyBuilder1.EndGeography();
Shape1 = geographyBuilder1.ConstructedGeography;
SqlGeography Shape2 = new SqlGeography();
SqlGeographyBuilder geographyBuilder2 = new SqlGeographyBuilder();
geographyBuilder2.SetSrid(4326);
geographyBuilder2.BeginGeography(OpenGisGeographyType.Polygon);
geographyBuilder2.BeginFigure(47.4275329011347, -86.8136038458706);
geographyBuilder2.AddLine(36.5102408627967, -86.9680936860962);
geographyBuilder2.AddLine(37.4928909385966, -80.2884061860962);
geographyBuilder2.AddLine(47.4275329011347, -86.8136038458706);
geographyBuilder2.EndFigure();
geographyBuilder2.EndGeography();
Shape2 = geographyBuilder2.ConstructedGeography;
SqlGeography IntersectedShape = Shape1.STIntersection(Shape2);
I believe you can draw shapes with the new GeoRSS feature - it supports line, boxes, polygons and circles. GeoRSS was designed as a lightweight, community driven way to extend existing feeds with geographic information. Also check out GeoJson

What is the projection used by MKMapView?

I am storing a number of point features inside a SQLite database for display inside of MKMapView and I would like to precompute and store coordinates in the the projection of the map instead of in lat/lon (for index/performance reasons). However, I am creating this database externally (in Python), so I cannot simply use the MKMapPointForCoordinate() function. What is the projection used by MKMapView? The documentation states that it is a mercator projection but it does not appear to be the web mercator projection that I expected. How can I compute an X/Y coordinate from a Lat/Lon ?
Personally I feel you're approaching the problem from the wrong angle - you say don't want to store latitude/longitude co-ordinates for 'index/performance reasons'. What are these reasons?
Storage, indexing, and querying of geographic coordinates inside of a database is not an unusual problem. They are simply two decimal numbers, making indexing and querying very straightforward.
Apple also tell you only to save co-ordinates rather than MKMapPoints in their own documentation:
When saving map-related data to a file, you should always save coordinate values (latitude and longitude) and not map points.
Perhaps you could share some of the issues you're having saving co-ordinates to a database?
I'm pretty sure it is webmercator because the overlay tiles I have produced are in that projection and they fit. But like other conmenters said, you're advised to store them in lat/long. If you want to query results in a grid lat/long provide a pretty good starting point. The grid isn't square as you leave the equator but it is rectangular.

How do I optimize point-to-circle matching?

I have a table that contains a bunch of Earth coordinates (latitude/longitude) and associated radii. I also have a table containing a bunch of points that I want to match with those circles, and vice versa. Both are dynamic; that is, a new circle or a new point can be added or deleted at any time. When either is added, I want to be able to match the new circle or point with all applicable points or circles, respectively.
I currently have a PostgreSQL module containing a C function to find the distance between two points on earth given their coordinates, and it seems to work. The problem is scalability. In order for it to do its thing, the function currently has to scan the whole table and do some trigonometric calculations against each row. Both tables are indexed by latitude and longitude, but the function can't use them. It has to do its thing before we know whether the two things match. New information may be posted as often as several times a second, and checking every point every time is starting to become quite unwieldy.
I've looked at PostgreSQL's geometric types, but they seem more suited to rectangular coordinates than to points on a sphere.
How can I arrange/optimize/filter/precalculate this data to make the matching faster and lighten the load?
You haven't mentioned PostGIS - why have you ruled that out as a possibility?
http://postgis.refractions.net/documentation/manual-2.0/PostGIS_Special_Functions_Index.html#PostGIS_GeographyFunctions
Thinking out loud a bit here... you have a point (lat/long) and a radius, and you want to find all extisting point-radii combinations that may overlap? (or some thing like that...)
Seems you might be able to store a few more bits of information Along with those numbers that could help you rule out others that are nowhere close during your query... This might avoid a lot of trig operations.
Example, with point x,y and radius r, you could easily calculate a range a feasible lat/long (squarish area) that could be used to help rule it out if needless calculations against another point.
You could then store the max and min lat and long along with that point in the database. Then, before running your trig on every row, you could Filter your results to eliminate points obviously out of bounds.
If I undestand you correctly then my first idea would be to cache some data and eliminate most of the checking.
Like imagine your circle is actually a box and it has 4 sides
you could store the base coordinates of those lines much like you have lines (a mesh) on a real map. So you store east, west, north, south edge of each circle
If you get your coordinate and its outside of that box you can be sure it won't be inside the circle either since the box is bigger than the circle.
If it isn't then you have to check like you do now. But I guess you can eliminate most of the steps already.