I have a collection of polygons in a table stored as type polygon. For a given point, I need to determine the distance to the nearest polygon border (i.e., I need to trigger a behavior change when a border is crossed - either entering or leaving a polygon, and I want to know how far the point has to move before this condition may be met).
The ST_Distance() function can provide the distance between a point and a polygon's nearest edge. Using this function I can compare a specific point against all polygons in my table and sort to find the distance to the closest one.
The ST_Distance() function, however, only returns a distance value for polygons that do not contain the point provided. If the point provided is within a polygon, the returned distance is 0 - but I need the distance to the nearest edge in this case, as well.
How can I get the distance to the nearest edge of all polygons, including those my point may be contained within?
Related
I am writing some code which clusters markers on a Google map. The clustering algorithm relies on knowing how many degrees latitude and longitude are currently visible to the user, as I break the map into a grid of n/map_pixels_width x n/map_pixels_height squares and need to know how many degrees of lat/lon are in each square to know which square each marker point belongs to.
Under normal circumstances where the map does not wrap this is relatively easy to calculate using getBounds() on the Google Map object and doing the calculations to figure out the latitudinal and longitudinal distances between the returned North-East and Sout-West points. Where I'm running into issues is where the map is zoomed out to the extent that it wraps the entire Earth > 1 times. For example, I can zoom out the map so that the entire Earth is "tiled" 5 times over which equates to 360 * 5 = 1800 longitudinal degrees and, but then the call to getBounds() no longer provides useful information:
m.getBounds().getNorthEast().lat()
88.31833020528785
m.getBounds().getNorthEast().lng()
180
m.getBounds().getSouthWest().lat()
-88.5485785544835
m.getBounds().getSouthWest().lng()
-180
Basically, the longitudes getBounds() reports are just the min and max for one whole globe which says nothing about how many times the Earth is repeated. Although Google Maps doesn't tile the map vertically (it just inserts gray filing space if zoomed out far enough), I have conceptually the same problem -- I need to know how many total degrees of space the vertical area would consume.
Is there a way to get the total number of visible longitudinal degrees?
So based on this answer to another question, I found a (hackish) way to solve this. Basically, the Google Maps OverlayView class has a getProjection() method returning a MapCanvasProjection object, which in turn has a getWorldWidth() method which returns the width of the world at the current zoom level in pixels. So the way to solve the problem then is to:
Add a dummy OverlayView to the map that doesn't actually present an overlay.
Get the overlay's projection.
Get the world width from the projection.
Calculate the number of visible longitudinal degrees as pixel_width_of_map_element / world_width_in_pixels * 360
It would be better if there were a way to do this without creating a dummy overlay, but this method seems to work.
There is a way to convert a distance (in kilometers) into a float zoom level of a GMSCameraPosition?
I have an app where the user specifies a diameter (in kilometers) that he wants to see around the gps checkmark (his position). So, I need to convert that distance (like, 30 km) into a zoom level that fits that distance in kilometers.
I'm very new with Google Maps and didn't find a way to that until now.
Thanks in advance.
For calculating with GPS coordinates have a look here. (Especially the section "Destination point given distance and bearing from start point").
When you have your coordinates you can use the fitBounds-method of the Map-object which does exactly what you want.
I try to create a WordPress plugin, that will use the Google maps directions in combination with bounds.
This is an application for km based charge, that has low cost/km into the bounds and high cost/km outside the bounds.
So the question is:
Is there a way to know what is the distance from within the bounds point to the bounds border
Is there a way to know what is the distance from a point outside the bounds to the bounds borders?
Note that the bounds area will be made with poligons. I have find the way to check if a point in the poligon or outside the poligon.
The problem is what I describe above in the list.
Yes, there's a function in the API that measures distances between LatLngs.
It's called : computeLength() in the geometry library.
Please read this
https://developers.google.com/maps/documentation/javascript/geometry
Distance and Area Functions
The distance between two points is the length of the shortest path between them. This shortest path is called a geodesic. On a sphere all geodesics are segments of a great circle. To compute this distance, call computeDistanceBetween(), passing it two LatLng objects.
You may instead use computeLength() to calculate the length of a given path if you have several locations.
Distance results are expressed in meters.
To compute the area (in square meters) of a polygonal area, call computeArea(), passing the array of LatLng objects defining a closed loop.
i have elasticsearch database of geo objects.
I want to search the nearest objects around some geo_point in custom distance and then display them on google map.
using default elasticsearch filters - geo_distance filter, it works fine, but the problem is that returned pins are in eliptical range, not in circular range (as usually when you search anything in X km RADIUS) maybe screenshots would help to understand.
I don't know where the problem can be, google maps only takes the result that elastic returns - set of pins to display
Does anybody know anything about this issue?
When geo points are represented as arrays in elasticsearch they are following GeoJSON format, which is [lon, lat]. So, from elasticsearch perspective, your center pin is not in Paris but just off the coast of Somalia.
Based on your screenshots I came up with a couple of points in your circle.
Assuming we have centre coordinate of lat:48.853647, lon:2.347894, and a point at lat:48.853647 lon:2.32 (a point at the same lat but further west):
Using distance_type:plane the distance calculated is ~ 3.1km, while using distance_type:arc the distance is ~ 2km.
If we use a different point, lat:48.8717 lon:2.347894 (further north of the centre point, but some lon), using distance_type:plane the distance is ~ 2km and using distance_type:arc it is also ~ 2km.
Consequently if the filtered distance was say 2.5km, then using distance_type:arc would correctly include both points, while using distance_type:plane would only include the northern point, giving the elliptical shape.
I have a requirement to calculate the centroid or geodesic midpoint of
when a user clicks in between the lat/long grid crossing.
The crossing forms a square in most parts of GE and sometimes
elongated rectangles. This is due to the shape of the earth of course.
I'm looking for a valid mathematical formula that would allow a user
to click anywhere in between this grid and then an accurate function
(in Javascript or server side code) that would take an assumed grid
resolution (say 1km intervals for this discussion) and the input
coordinates that should return a centroid coordinate within that
graticule grid.
To clarify please take a look at the attached image to my google group post:
http://google-earth-api.googlegroups.com/web/Picture+5.png?gda=h5oFPz8AAAD315KpovipQeBwdfGpmW3ZhBc9PTADwYa-n193hZ6AItFmHuno63c7phcEXYVuRA6ccyFKn-rNKC-d1pM_IdV0&gsc=sz6bbAsAAABBKF7YXWYyc4GmXg-QruHj
What I need to be able to do is if a user clicks anywhere in this grid
square, I need to find the centroid or center point of that grid
intersection/square or at least the bounding grid coordinates (that make the square).
If we assume that the grid is UTM standard and has a max resolution of
1km (or make this a parameter), I need to detect the four other points
nearby and then calculating the centroid is not as difficult.
I welcome any feedback you all may have and appreciate it.
I don't have a simple way of letting a user click anywhere on the grid and finding the grid bounding coordinates (making a square of 4 coordinates) or the centroid / midpoint of the graticule grid square necessary.
One thought is to use assumptions as much as possible using a reference such as UTM coordinate reference.
If I assume that the grid is X degrees wide, can we have a pure javascript function take any input coordinate and return for me the bounding graticule coordinates in Decimal Degrees?
Another thought I had was to create the grid in a geo-spatial layer to take any input coordinate and return the nearest centroid of the graticule?
Does this make sense?
Thanks!
Omar
this seems to be a rounding problem
1) your users click anywhere on the map
2) you want to trap the click to read out the actualLat/actualLon values
3) and round it down to the nearest discrete grid values minLat/minLon
4) then you want to calculate and return in some way the midpoint of the grid as
midLat = minLat + deltaGridLat/2;
midLon = minLon + deltaGridLon/2;
ad 2) look up the .js file in my Maidenhead locator viewer - you can surely reuse code for initializing the GE event handler and the handler for a mouse click (note I am trapping a RIGHT click)
The event handler essentially takes the user chosen location, passes it to function CoordToLoc() where the coordinates are converted to a Maidenhead locator string, which then is displayed in field inpLocator on the web page.
ad 3) this depends on your grid - if it is bound to discrete Lat/Lon values (as in my case) it's as simple as rounding. If the grid points are defined as distance offsets in km from a reference point, the simple approach would be to divide your argument by the unity length and disregard the decimal places
Hope this helps
Good luck
MikeD