I want to search the Places API by specifying bounding latitudes and longitudes instead a single latitude/longitude coordinate and a radius.
As an example, I'd like to be able to query the API for all results within the graticule bounded by 60N in the south, 61N in the north, 1E to the west and 2E to the east.
Is this possible?
It doesn't look like the Place Search API exposes this capability.
You can work around this with a little math by providing a radius r such that the approximately square area you are interested in with side length s is inscribed by the circlular search area. This will provide you with a superset of the results you are looking for which you can then filter in the application layer.
Say I'm interested in the approximately square area centered on lat[60.5], lng[0.5] where the square has side length 500 meters.
The diagonal of the square is:
(2 * s^2)^(1/2)
(2 * 500^2)^(1/2) ~= 707
The diagonal of this square is the diameter of the circumscribing circle. To get the radius, we divide by 2:
707 / 2 ~= 353
In this manner you can approximately accomplish your task.
Related
I have 1000 of 2D gray-scale images and would like to cluster them in python in a way that images with more similarities stay in same group. The images represents simple geometrical shapes including circles, triangle etc.
If I wan to flatten each image to have a vector and then run the clustering algorithm, it would be very complicated. The images are 400*500, so my clustering training data would be 1000*200000 which means 200000 features!
Just wondering if anyone has come across this issue before?
This is a similar question to this one
Read my answer
Of course you don't use each picture as a feature.
In your case I would recommend features like:
Find corners and calculate their number
Assuming each edge is a straight line - do a histogram of orientations. In each pixel calculate the derivative angle atan(dy,dx), take the strongest 1% of derivative pixels and do a histogram. The amount of peaks in the histogram will correspond to amount of edges (will cluster triangles, squares, circles, etc)
Use connected components analysis to calculate how many shapes you have in the image. Calculate the amount of holes in each shape. Calculate the ratio between the circumference and the area o the shape. For geometrical shapes, geometrical features work extremely well
As you asked in the comment I am adding more info for issue 2.
Please read more about HOG feature here. I assume your are familiar with that is an edge in the image and what a gradient is. Imagine you have a triangle in the image. Only Pixels that lie on the edges of the shape will have a high gradient. Moreover you expect that all the gradients devide into 3 different directions, one for each edge. You don't know in which direction since you don't know the orientation of the triangle but you know that there should be 3 directions. With a square there would be 2 directions and with circle there will not be a clear direction. You want to count the amount of directions. Use the following steps. First find the pixels which have a high gradient value. Say from the entire image there is only 1000 such pixels (they lie on the edges of the shape). For each pixel calculate the angle of the gradient. So you have 1000 pixels, each may have an angle of [0..179] (Angle of 180 is equal to 0). There are 180 different angles. Lets assume that in order to reduce noise you don't need the exact angle but +- 1 degrees. So each angle is divided by 2 and rounded to the nearest integer. So totally you have 1000 pixels, each having only 90 options for different angle. Now make a histogram of angles. If the shape was a circle you expect that roughly ~11 (=1000/90) pixels will fall into each bin of the histogram. If it was a square you expect the histogram to be largely empty except for 2 bins with a very high amount of pixels in it and the bins being at distance of 45 from each other. Example: bin 13 has 400 pixels in it, bi 58 has
400 pixels in it and the rest 200 are noise split somehow in the other bins. Now you know that you are facing a square and you also know its rotation in the image.
If it was a triangle you expect 3 large bins in the histogram.
I have a webpage that I am using a Google Map on. When the user drags the map and lets go, I need to query a server for all data points that fall within the bounds of the visible region of the map. I can quite easily get the North-East and South-West coordinate of the visible region of the map through the javascript API, essentially providing a bounding rectangle. However on the server, I am relying on a database whose geographic query API only supports queries in the form of a center point and a radius. So basically what I would like to do is figure out the minimum radius circle I would need to at least encompass the North-East and South-West points.
The simplest algorithm I thought of involved finding the center point between the NE and SW coordinate and then measuring the radius as the distance from the center point to either the NE or SW coordinate. In a simple euclidean space I'd be comfortable doing this, but I think I'd probably get something wrong with the Earth's non-flat coordinate system. I haven't even been able to convince myself that if I knew that centerpoint that the distance would be the same between the center and NE and the center and SW.
I've come across algorithms for smallest circles on a flat 2D surface and also algorithms describing the opposite i.e. bounding box from circle center and radius. I haven't come across a concise algorithm for this particular problem though.
I assume what you call the east-west and north-south coordinates are the longitude and latitude. You can convert them to Cartesian points and find the midpoint between the edge points of your region. This will yield a point C' below Earth's surface with the same latitude and longitude as your centre point C. (This will only work if the difference of your longitudes is smaller than 180°, however; otherwise you'll get a point on the opposite side of the earth, but with the same latitude.) If you need Cartesian coordinates for your centre point, you can project C' onto the surface by adjusting the radius to find your new centre point.
The distance bewteen the two points on the surface of Earth can be calculated with the great-circle disnatce formula.
Transformation is easy if you assume that Earth is a perfect sphere with radius R = 6373 km:
x = R * cos(lat) * cos(lon)
y = R * cos(lat) * sin(lon)
z = R * sin(lat)
and back:
lon = atan2(y, x)
lat = atan2(z, r) with r = sqrt(x*x + y*y)
(But Earth does not have a constant radius, so you might want to use a better coordinate system, maybe ECEF as explained in this answer if you need more precision.)
My first thought was to find your midpoint in terms of longitude and latitude, which should be okay if you take care of wrapping for the latitude. Then you calculate your distance accpording to the great-circle formula. But averaging the longitudes and latitudes does not seem to be sensible if your map region includes a pole.
Im working on maps, in which I need to show nearest locations from the user GEO coordinates. These nearest location should be within 1km or user specified radius.
Instead of running a query and calculating the distance for all geo coordinates in my DB, I have narrow down my selection by choosing country - state - city. However I still dont find this way is more efficient.
Could someone suggest me the better way to solve this issue.
What you are looking for is the Haversine Formula, based on co-ordinates. You will have to compare the distance between co-ordinates of the location and the points of interest using Haversine. Then if the result is below the threshold (1km) you display the point of interest.
See this Wikipedia article if you need a quick and fairly accurate estimation of distance using coordinates.
I suggest you work out max and min latitude and max and min longitude so that all points that have both latitude and longitude between the respective min and max lie in the 2km x 2km square which is centered on the current point. Then you can just run a query to select which geopoints are closest. To do that, you need to work out what the ratio is between a km and a degree of latitude, and the ratio between a km and a degree of longitude. The answer is
1 degree of latitude = 6371000 * Math.PI / 180 metres
1 degree of longitude = Cos(Latitude) * 6371000 * Math.PI / 180 metres
because 6371000 is average earth radius in metres. Some points (i.e. at the corners of the square) will be more than 1km away, however you could then work out exact distance of each point within that square if the answer needs to be accurate. NB: a lot of Cosine functions take radians rather than degrees, so make sure to get the units right!
Given a list of coordinates, how do I calculate the minimum bounding rectangle (MBR), avoiding the global gotchas described in the
Unlocking the Mysteries of the Bounding Box?
Google Maps API method fitBounds() seems to be handling the gotchas well.
Edit:
Using an example from the article above, let's say I have to calculate a bounding box for two locations in the imaginary country of Boxtopia: point A(170, 40) and point B(-170, 50). If construct my bounding box using xmin, ymin as the southwest corner and xmax, ymax as the northwest corner, I'll get (-170, 40) and (170, 50) respectively, a box which would span 340 degrees instead of minimum 20 degrees.
For the 1D problem, you are looking for the largest empty interval on a 'circle'. Sorting the data, and then looking for the largest gap will provide the answer in O(N.log(N)).
One easy solution is to sort the x coordinates and incrementally add add 360 to the lowest values and see if it results in a smaller bound.
I have some GPS sample data taken from a device. What I need to do is to "move" the data to the "left" by, let's say, 1 to 5 meters. I know how to do the moving part, the only problem is that the moving is not as accurate as I want it to be.
What I currently do:
I take the GPS coordinates (latitude, longitude pairs)
I convert them using plate carrée transformation.
I scale the resulting coordinates to the longitudinal distance (distance on x) and the latitudinal distance (distance on y) - imagine the entire GPS sample data is inside a rectangle being bound by the maximum and minimum latitude/longitude. I compute these distances using the formula for the Great Circle Distance between the extreme values for longitude and latitude.
I move the points x meters in the wanted direction
I convert back to GPS coordinates
I don't really have the accuracy I want. For example moving to the left by 3 meters means less than 3 meters (around 1.8m - maybe 2).
What are the known solutions for doing such things? I need a solution that deviates at most by 0.2-0.5 meters from the real point (not 1.2 like in the current case).
LATER: Is this kind of approach good? By this kind I mean to transform the GPS coordinates into plane coordinates and back to GPS. Is there other way?
LATER2: The approach of converting to a conformal map is probably the one that will be used. In case of a small rectangle, and since there are not roads at the poles probably Mercator will be used. Opinions?
Thanks,
Iulian
PS: I'm working on small areas - so imagine the bounding rectangle I'm talking about to have the length of each side no more than 5 kilometers. (So a 5x5km rectangle is maximum).
There are two issues with your solution:
plate carrée transformation is not conformal (i.e. angles are not preserved)
you can not measure distances along lat or lon that way since that are not great circles (approximately you are off by a factor cos(lat) for your x).
Within small rectangles you may assume that lon/lat can be linearly mapped to x/y pairs but you have to keep in mind that a "square" in lon/lat maps to a rectangle with aspect ratio of approx cos(lat)/1.