Hi I've been following this tutorial http://googledevelopers.blogspot.com/2014/12/building-scalable-geofencing-api-on.html , and here's my problem,
I have a list of longitude and latitude coordinates that I've added to a JTS(Java Topology Suite) STR tree as Points.
Now I would like to send an area that is in the shape of a circle to the STR tree to find all of the Points that land in the circle.
Coordinate center = new Coordinate(entity.getLongitude(), entity.getLatitude());
GeometricShapeFactory gsf = new GeometricShapeFactory();
gsf.setCentre(center);
gsf.setNumPoints(20);
**gsf.setSize(320.0);**
Polygon poly = gsf.createCircle();
Coordinate[] coordinates = poly.getCoordinates();
//Create polygon from the coordinates.
GeometryFactory fact = new GeometryFactory();
LinearRing linear_ring = new GeometryFactory().createLinearRing(coordinates);
Polygon polygon = new Polygon(linear_ring, null, fact);
List<STRLeaf> items = strTree.query(polygon.getEnvelopeInternal());
However the result of the search sends back all of the data in the in the tree of longitude and latitude Points. When I lower the size of the circle below 320 then I receive no results from the search of the STR tree. Does anyone have experience with this ideally I would like to create the circle that finds all the points within a circle of ~7miles.
thanks for your time
It turns out that I had a silly mistake in the backend. When I added the items to the tree I mixed up the x and y coords, so that the Lat Longs were reversed. After switching them, I can now set the size of the circle to around 0.1 and it works well.
Related
I have two set's of longitude and latitude, i am desperately trying to figure out how many meters point A is displaced from point B, horizontally and vertically.
My goal would be have to +/-X and +/-Y values - I already have the shortest distance between the two points via Location.distanceBetween()....i thought i could use this with the Location.bearingTo() to find the values im looking for via basic trigonometry.
My thinking was i could use the bearing as angle A, 90 degrees as angle C and legnth of Side C (distanceBetween) to calculate the legnth of side A (x axis) and B (y axis) but the results were underwhelming to say the least lol
//CALCULATE ANGLES
double ANGLE_A;
ANGLE_A = current_Bearing; //Location.bearingTo()
ANGLE_A = ANGLE_A*Math.PI/180; //CONVERT DEGREES TO RADIANS
double ANGLE_C;
ANGLE_C = 90; // Always Right Angle
ANGLE_C = ANGLE_C*Math.PI/180; //CONVERT DEGREES TO RADIANS
double ANGLE_B;
ANGLE_B = 180 - ANGLE_A - ANGLE_C; // 3 sides of triangle must add up to 180, if 2 sides known 3rd can be calced
ANGLE_B = ANGLE_B*Math.PI/180; //CONVERT DEGREES TO RADIANS
//CALCULATE DISTANCES
double SIDE_C = calculatedDistance; //Location.distanceTo()
double SIDE_A = Math.sin(ANGLE_A) * SIDE_C /Math.sin(ANGLE_C);
double SIDE_B = Math.sin(ANGLE_B)*SIDE_C/Math.sin(ANGLE_C);
What im noticing is that my bearing changes very little between the two points regardless of how we move, though mind you im testing this at 10 - 100m distance, its always at 64.xxxxxxx and only the last few decimals really change.
All the online references i can find always look at computing the shortest path, and although this awesome site references x and y positions it always ends up combining them into shortest distance again
Would SUPER appreciate any pointers in the right direction!
Since the earth is not flat, your idea with 90 degree angles will not work properly.
What might be better, is this.
Lets say your 2 known points A and B have latitude and longitude latA, longA and latB, longB.
Now you could introduce two additional points C and D with latC = latA, longC = longB, and latD = latB, longD = longA, so the points A, B, C, D form a rectangle on the earth's surface.
Now you can simply use distanceBetween(A, C) and distanceBerween(A, D) to get the required distances.
It may be possible to utilize Location.distanceBetween(), if following conditions meet,
the points are located far apart from polar regions and
distance is short enough (compared to radius of the Earth).
The way is very simple. Just fix either longitude or latitude and vary only the other. Then calculate distance.
Location location1 = new Location("");
Location location2 = new Location("");
location1.setLatitude(37.4184359437);
location1.setLongitude(-122.088038921);
location2.setLatitude(37.3800232707);
location2.setLongitude(-122.073230422);
float[] distance = new float[3];
Location.distanceBetween(
location1.getLatitude(), location1.getLongitude(),
location2.getLatitude(), location2.getLongitude(),
distance
);
double lat_mid = (location1.getLatitude() + location2.getLatitude()) * 0.5;
double long_mid = (location1.getLongitude() + location2.getLongitude()) * 0.5;
float[] distanceLat = new float[3];
Location.distanceBetween(
location1.getLatitude(), long_mid,
location2.getLatitude(), long_mid,
distanceLat
);
float[] distanceLong = new float[3];
Location.distanceBetween(
lat_mid, location1.getLongitude(),
lat_mid, location2.getLongitude(),
distanceLong
);
double distance_approx = Math.sqrt(
Math.pow(distanceLong[0], 2.0) + Math.pow(distanceLat[0], 2.0)
);
Compare distance[0] and distance_approx, check whether accuracy meets your requiement.
If your points are close enough, you may easily calculate x-y distances from latitude / longitude once you know that 1 degree of latitude is 111km, and one degree of longitude is 111km * cos(latitude):
y_dist = abs(a.lat - b.lat) * 111000;
x_dist = abs(a.lon - b.lon) * 111000 * cos(a.lat);
For short distances we could easily ignore that earth is not exactly a sphere, the error is approximately 0.1-0.2% depending on your exact location.
There is no valid answer to this question until you define what projection.
The azimuth of a "straight" line varies along the route unless you are travelling exactly due south or due north. You can only calculate the angles at each node, or azimuth at a specific point along the route. Angles at the nodes will not add up to 180° because you're referring to an ellipsoidal triangle, and calculating an ellipsoidal triangle is a multiple-step process that in all honesty, is better left to the libraries out there such as OSGEO.
If you want to fit the geometry to a plane Cartesian, it is usually using the Lambert projection for areas mostly long on east and west directions, and Transverse Mercator on longer north to south projections. The entire Earth is mapped in the UTM (Universal Transverse Mercator) that will give you Cartesian coordinates anywhere, but in no case will you get perfect Eucldian geometry when dealing with geodetics. For instance, if you go south 10 miles, turn left 90° and go east for 10 miles, turn left 90° again, you can be anywhere from 10 miles from your starting point, to exactly back to where you started, if that point happened to be the North pole. So you may have a mathematically beautiful bearing on the UTM coordinate plane, but on the ground, you cannot turn the same angles as the UTM geometry indicates and follow that same path on ground. You will either follow a straight line on the ground and a curved line on a cartesian plane, or vice-versa.
You could do a distance between two points on the same northings and separately, the same eastings, and derive a north distance and an east distance. However, in reality the angles of this triangle will make sense only on paper, and not on the globe. If a plane took off at the bearing calculated by such a triangle, it would arrive in the wrong continent.
Good day.
I would like to convert a screen x,y pixel location (the location a user tapped/clicked) to a lon/lat location on a map.
The current screen location is in a bounding box, of which you have the top left most and bottom right most lon/lat values.
When the screen is not rotated it is quite simple to translate the x/y position to the lon,lat values:
Let mapboudingbox[0,1] contain top left most lat/lon
mapboundingbox[2,3] contains bottom right most lat/lon
Then the degrees per pixel width = abs(lon2 - lon1)/ screenWidthInPixels
Then the degrees per pixel height = abs(lat2 - lat1)/ screenHeightInPixels
From this you can then get Lon/Lat as follow:
float longitude = ((touchXInPixels) * degreesPerPixelWidth) + mapBoundingBox[1];
float latitude = ((touchYPixels) * degreesPerPixelHeight) + mapBoundingBox[0];
This is easy enough. The problem that I have is calculating the Lat/Lon values when the screen is rotated, i.e:
From this, you can see that the screen has now been rotated by an angle Ө. -180 < Ө < 180
So let's assume the user clicks/taps on the screen FQKD at point Sx,Sy. How can I get the new lon/lat values where the user clicked, assuming that we have point Z and R in Lat/Lon, as well as the angle Ө, as well as the screen height and width in pixels?
Any and all help will be much appreciated!
I would just modify standard rotation and scale algorithm for 2D. Read a bit here:
2dTransformations.
The easiest way to achieve this is with matrices.
A 3x3 matrix can describe the rotation, translation & scale in 2D space.
Using this matrix you can project your map image on to the screen area. And using the inverse of the matrix, you can take a point in screen space to map space.
Pseudocode: (as you don't care what language)
Build your matrix:
var matrix = Matrix.newIdentity();
matrix.postAppendTranslate(tx, ty);
matrix.postAppendScale(zoom);
matrix.postAppendRotate(rot);
Render map image using that matrix.
To reverse a press:
var inverseMatrix = matrix.inverse();
var point = new float[]{touchPointX, touchPointY, 1};
var transformedPoint = inverseMatrix.multiply(point);
var mapX = transformedPoint[0];
var mapY = transformedPoint[1];
I have a circle on my map. Now I want to detect if the user (or me) is inside the circle.
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(14.635594, 121.032962))
.radius(55)
.strokeColor(Color.RED)
);
I have this code:
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,ll);
Location.distanceBetween( pLat,pLong,
circle.getCenter().latitude, circle.getCenter().longitude, distance);
if( distance[0] > circle.getRadius() ){
Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
}
And on myLocationListener I have this:
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
pLong = location.getLongitude();
pLat = location.getLatitude();
}
It works correctly if I parameter inside distanceBetween is the coordinates of marker, however, the toast displays Outside even though my location is inside the radius.
Any ideas how to do this correctly? Please help. Thanks!
EDIT
I discovered something odd.
On the picture, you can see I have a textView above which has 5 numbers (circle Latitude, circle longitude, distance at index 0 , distance at index 1 , distance2). distance is a float array to store the distance between the center of the circle and the user location. I set the radius to 100, and I think the unit is meters, however, as you can see, the values at the distance array are : 1.334880E7 , -81.25308990478516 , -10696092987060547 . What is the formula for the computation of the distance? And also, 1.something times 10 raise to 7 is about 13 million which is really greater than 100. Please help its really confusing right now. According to documentation of Circle (The radius of the circle, specified in meters. It should be zero or greater.) and distanceBetween (Computes the approximate distance in meters between two locations) so I don't know why is this the result.
tl;dr? jsFiddle here - look at your console output.
Basically there're two ways to do this:
Check if the (marker of) the user is inside the Circle Bounds
Compute the distance between the user and the center of the Circle. Then check if it is equal or smaller than the Circle Radius. This solution needs the spherical library to work.
Circle Bounds
Just add a circle:
circle = new google.maps.Circle( {
map : map,
center : new google.maps.LatLng( 100, 20 ),
radius : 2000,
strokeColor : '#FF0099',
strokeOpacity : 1,
strokeWeight : 2,
fillColor : '#009ee0',
fillOpacity : 0.2
} )
and then check if the marker is inside:
circle.getBounds().contains( new google.maps.LatLng( 101, 21 ) );
At a first glance you might think this works. But it doesn't. In the background google (still) uses a rectangle, so everything inside the rectangular bounding box, but outside the circle will be recognized as inside the latLng bounds. It's wrong and a known problem, but it seems Google doesn't care.
If you now think that it would work with rectangular bounds, then you're wrong. Those don't work either.
Spherical Distance
The easiest and best way is to measure the distance. Include the spherical library by appending &library=spherical to your google maps script call. Then go with
google.maps.geometry.spherical.computeDistanceBetween(
new google.maps.LatLng( 100, 20 ),
new google.maps.LatLng( 101, 21 )
) <= 2000;
I know this question had been asked more than a year ago but I have
the same problem and fixed it using the distanceBetween static function of Location.
float[] distance = new float[2];
Location.distanceBetween(latLng.latitude, latLng.longitude, circle.getCenter().latitude,circle.getCenter().longitude,distance);
if ( distance[0] <= circle.getRadius())
{
// Inside The Circle
}
else
{
// Outside The Circle
}
Use GoogleMap.setOnMyLocationChange(OnMyLocationChangeListener) instead of LocationManager. This way you will get Locations that are the same as blue dot locations.
I have a list of lat/long coordinates that I would like to use to calculate an area of a polygon. I can get exact in many cases, but the larger the polygon gets, the higher chance for error.
I am first converting the coordinates to UTM using http://www.ibm.com/developerworks/java/library/j-coordconvert/
From there, I am using http://www.mathopenref.com/coordpolygonarea2.html to calculate the area of the UTM coordinates.
private Double polygonArea(int[] x, int[] y) {
Double area = 0.0;
int j = x.length-1;
for(int i = 0; i < x.length; i++) {
area = area + (x[j]+x[i]) * (y[j]-y[i]);
j = i;
}
area = area/2;
if (area < 0)
area = area * -1;
return area;
}
I compare these areas to the same coordinates I put into Microsoft SQL server and ArcGIS, but I cannot seem to match them exactly all the time. Does anyone know of a more exact method than this?
Thanks in advance.
EDIT 1
Thank you for the comments.
Here is my code for getting the area (CoordinateConversion code is listed above on the IBM link):
private Map<Integer, GeoPoint> vertices;
private Double getArea() {
List<Integer> xpoints = new ArrayList<Integer>();
List<Integer> ypoints = new ArrayList<Integer>();
CoordinateConversion cc = new CoordinateConversion();
for(Entry<Integer, GeoPoint> itm : vertices.entrySet()) {
GeoPoint pnt = itm.getValue();
String temp = cc.latLon2MGRUTM(pnt.getLatitudeE6()/1E6, pnt.getLongitudeE6()/1E6);
// Example return from CC: 02CNR0634657742
String easting = temp.substring(5, 10);
String northing = temp.substring(10, 15);
xpoints.add(Integer.parseInt(easting));
ypoints.add(Integer.parseInt(northing));
}
int[] x = toIntArray(xpoints);
int[] y = toIntArray(ypoints);
return polygonArea(x,y);
}
Here is an example list of points:
44.80016800 -106.40808100
44.80016800 -106.72123800
44.75016800 -106.72123800
44.75016800 -106.80123800
44.56699100 -106.80123800
In ArcGIS and MS SQL server I get 90847.0 Acres.
Using the code above I get 90817.4 Acres.
Another example list of points:
45.78412600 -108.51506700
45.78402600 -108.67972100
45.75512200 -108.67949400
45.75512200 -108.69962300
45.69795400 -108.69929400
In ArcGIS and MS SQL server I get 15732.9 Acres.
Using the code above I get 15731.9 Acres.
The area formula you are using is valid only on a flat plane. As the polygon gets larger, the Earth's curvature starts to have an effect, making the area larger than what you calculate with this formula. You need to find a formula that works on a the surface of a sphere.
A simple Google search for "area of polygon on spherical surface" turns up a bunch of hits, of which the most interesting is Wolfram MathWorld Spherical Polygon
It turns out that UTM just isn't able to get the extreme accuracy I was looking for. Switching projection systems to something more accurate like Albers or State Plane provided a much more accurate calculation.
I'm drawing within a servlet a ScatterPlot and serve it to the browser.
The user can now click somewhere on the plot and I want to determine what
datapoint of the scatter plot the user has pointed. From the mouse click of the
user I can determine on which pixel of the image he has clicked, but how can
I get from this info to the coordinates on the domain and range axis?
I found tipps how to do it, which uses the ChartPanel. But for serving it directly
to the browser I only use an instance of a JFreeChar object.
Anybody has a clue or an example how to do it?
Thanks,
Dieter
I think I have found a solution. For the solution I need to get my chart again,
so I either have to create it a new or to save it somehow. But when I have a reference
to that chart the solution is as following:
JFreeChart chart = functionWhichRetrievesTheChart();
ChartRenderingInfo info = new ChartRenderingInfo();
// PLOT_SIZE is the size if the graph and has to be the same size as the original drawn chart.createBufferedImage(PLOT_SIZE, PLOT_SIZE, info);
graph, otherwise the pixel position points to somewhere else
PlotRenderingInfo plotInfo = info.getPlotInfo();
XYPlot plot = (XYPlot)chart.getPlot();
Point p = new Point(x,y); // x and y are the pixel positions
// this is the domain value which belongs to the pixel position x
double domain = plot.getDomainAxis().java2DToValue(p.getX(), plotInfo.getDataArea(), plot.getDomainAxisEdge());
// this is the range value which belongs to the pixel position y
double range = plot.getRangeAxis().java2DToValue(p.getY(), plotInfo.getDataArea(), plot.getRangeAxisEdge());