Find if location is land or water in WorldWind - java

I know in WorldWind Java you can find out the elevation and a particular location with something like this:
public Double getPositionElevationMeters(Double lat, Double lon) {
double elevation = getWorldWindCanvas().getModel().getGlobe()
.getElevation(Angle.fromDegrees(lat), Angle.fromDegrees(lon));
return elevation;
}
Is there a way to figure out if that lat/lon is actually a major body of water or land pro-grammatically? I've taken a "blind" approach of just considering elevation less than 0 to be water, but that's obviously not ideal.
I'd even use another library that would give me this information; I just need it to work offline.

You could possibly use a data source such as this from which you should be able to determine the polygons for all countries on Earth. Antarctica has also been added to that data set. This would get you most of the way there, depending on what you define as a "major" body of water.
From there, you can use GeoTools to import the shape data and calculate which polygons a given lat/lon pair fall in to. If none, then it is probably an ocean.
The following pseudocode illustrates the logical flow:
// pseudocode, not the actual GeoTools API
boolean isWater(Coordinate point)
{
ShapeDataStore countryShapes = loadShapeData("world.shp");
GeoShape shape = countryShapes.findShapeByPoint(point);
if (shape == null)
return true // not a country or Antarctica, must be international waters.
else
return false;
}
edit
See this answer for an answer to a similar question that describes this process in a bit more detail.

Related

How to get the intersecting points from shape in ElasticSearch

I have stored a route in ElasticSearch as a Polygon. Now I have a circle (A point and a radius), I'am able to check the circle points intersects the polygon or not (Below is the code I used).
Question: How can I get the points in the route which intersects the circle ?
public Boolean isMatchingDoc(Long elasticDocId, Double latitude, Double longitude, Long radius) {
Coordinate origin = new Coordinate(latitude, longitude);
ShapeBuilder circleShapeBuilder = ShapeBuilder.newCircleBuilder().center(origin).radius(radius,
DistanceUnit.METERS);
GeoShapeQueryBuilder geoShapeQueryBuilder = QueryBuilders.geoShapeQuery("route", circleShapeBuilder);
SearchRequestBuilder finalQuery = client.prepareSearch(INDEX).setTypes(TYPE)
.setQuery(QueryBuilders.termQuery("_id", elasticDocId)).setPostFilter(geoShapeQueryBuilder);
SearchResponse searchResponse = finalQuery.execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
if (searchHits.getTotalHits() > 0) {
return true;
}
return false;
}
I guess you are aware that with elasticsearch, you can query for polygons that intersect a given circle? See https://www.elastic.co/guide/en/elasticsearch/guide/current/querying-geo-shapes.html.
There are two reasons why this may not help you:
Your routes are not polygons, but lines.
You want to know the exact points of the intersection, if I read your question correctly.
Elasticsearch probably cannot solve this problem for you conveniently. It might be possible to solve if you would store all of your line-segments separately instead of in one huge polygon per route. Each line-segment then would have to bear an attribute that references the route it belongs to. Does that approach sound feasible to you?
Anyways, I would recommend you to look into the topic of "spatial databases":
Spatial databases are optimized for indexing and searching in a geometric space. Well-know databases such as PostgreSQL and MongoDB feature plugins/extensions for spatial indexing. I'm not sure what to recommend, but the MongoDB geospatial API looks promising for example, as it allows querying for intersection - and it supports lines as well as polygons.

ELKI get clustering data points

How do I get the data points and centroid that are in a kmeans (llyod) cluster when I use elki?
Also could I plug in those points into one of the distance functions and get the distance between any two of the points?
This question is different, because the main focus of my question is retrieving the data points, not custom data points. Also the answer on the other thread is currently incomplete, since it refers to a wiki that is not functioning at the moment. Additionally I would like to know specifically what needs to be done, because the documentation on all of the libraries is a bit like a wild goose chase and it would be greatly appreciated that if you know/understand the library that you would be direct with the answer so that others with the same problem could also have a good solid reference to refer to, instead of trying to figure out the library.
A Cluster (JavaDoc) in ELKI never stores the point data. It only stores point DBIDs (Wiki), which you can get using the getIDs() method. To get the original data, you need the Relation from your database. The method getModel() returns the cluster model, which for kmeans is a KMeansModel.
You can get the point data from the database Relation by their DBID,
or compute the distance based on two DBIDs.
The centroid of KMeans is special - it is not a database object, but always a numerical vector - the arithmetic mean of the cluster. When using KMeans, you should be using SquaredEuclideanDistanceFunction. This is a NumberVectorDistanceFunction, which has the method distance(NumberVector o1, NumberVector o2) (not all distances work on number vectors!).
Relation<? extends NumberVector> rel = ...;
NumberDistanceFunction df = SquaredEuclideanDistanceFunction.STATIC;
... run the algorithm, then iterate over each cluster: ...
Cluster<KMeansModel> cluster = ...;
Vector center = cluster.getModel().getMean();
double varsum = cluster.getModel().getVarianceContribution();
double sum = 0.;
// C++-style for loop, for efficiency:
for(DBIDRef id = cluster.getIDs().iterDBIDs(); id.valid(); id.advance()) {
double distance = df.distance(relation.get(id), center);
sum += distance;
}
System.out.println(varsum+" should be the same as "+sum);

Check if a position is clicked using HashMap

I'm writing a simple program and want to know if an approximate position is clicked. I've got a hashmap with the position as key value and want to display a currently invisible object if the user clicks close enough to the position of the object - not just right at it. The position class just holds an x and a y value.
HashMap<Position, Place> places = new HashMap<>(); //Assume this is populated
#Override
class WhatIsHere extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
Place place = places.get(new Position(me.getX(), me.getY()));
if (place != null) {
place.setVisible(true);
} else {
System.out.println("Nothing there");
}
}
}
This bit of code finds the place if you click right on it though I don't know how to look for, say, me.getX()+-10 and find objects in that range.
Do I need to set four ints holding x-10 and x+10 etc. and just loop through all the positions inbetween? It seems awfully dumb to do it that way.
I dislike exercises that require use of a particular collection, regardless of whether it is the best choice. One of the most important things to learn about the collections, and more generally about data structures, is picking which to use for a given job.
However, I understand you have to use HashMap. Here is one way to do it.
Divide the space up into small squares. Identify each square by e.g. the Point at the minimum x and minimum y. Create a HashMap that maps the square that are near at least one of your objects to the list of nearby objects.
To look up a point, calculate the Point identifying the square containing it. Look up that Point in the map. If it is not present, your point is not near any object. If it is present, check your point against each object in the list according to your nearness rules.
For some configurations of your objects, you may be able to ensure that each square is near at most one object. If so, you can replace the list with the object.
You might want to use TreeMap and you would be able to get a sub map which seems to be what you are looking for.

Generating knots procedurally (and visualizing them)

I'm looking for an algorithm that provides a way to generate graphical representations of knots (either 2D or 3D, but the former, with vector graphics is preferable).
I've seen many links to knot theory in general, spanning from punctual references to general information.
Before trying to devise something from scratch by myself, I'd like to know about the existence of some existing software that lets you not only represent them (in memory) but visualize in some of their graphical representations (there are many). It could come in the form of a library, or a simple function, or even a pseudocode algorithm that tries to specify how to properly draw a know on screen.
As the previous link suggests, there is a package in Wolfram Mathematica, named KnotTheory that does that (in an almost complete way). However, it is not portable, nor free software and accessing its modules would be very cumbersome for me (a free implementation in Java, just to name a language, but each language is fine, would be be ideal from the portability and openness perspectives).
I've seen that many softwares are available (but most of them are old and not reachable or usable).
Do you have some good pointers to share?
UPDATE:
Since two votes to close this question have appeared, I am restating it in a more pragmatic and clear way: are there algorithms to draw and generate knots? has something been already implemented
UPDATE 2 (for reopening)
The graphical representation could be a 3D rendered object or a 2D svg graphics (I am abstracting from it since I am looking forward a programming language as Processing (or the same Mathematica itself) that provides you the primitives to draw curves (splines, beziers, etc) on screen (and then export them to raster or vector graphics).
The algorithm shall take one knot parametrization as input (ie, if we are talking about knots described by their crossing properties, their values is what is needed), returning one of the graphical representation above (ie even a sequence of points in a 2d space). That is, any parametrization is fine, my objective is just to get introspection on how to draw knots so to get ONE algorithm that does that in a particular way, leading to a particular representation, would be fine (Mathematica's lib seems to be able to draw it in so many representations).
Something like this?
void setup() {
size(300, 300, P3D);
}
void draw() {
background(36, 10, 28);
int f = frameCount%360;
translate(width/2, height/2);
if (frameCount >= 360 && frameCount <= 1080 )
rotateY(radians(f));
stroke(0);
drawKnot(40);
translate(knotX(f)*40, knotY(f)*40, knotZ(f)*40);
noStroke();
fill(180,50,145);
sphere(10);
}
void drawKnot(float sz) {
stroke(200);
for (int i = 0; i < 360; i++) {
point(knotX(i)*sz, knotY(i)*sz, knotZ(i)*sz);
}
}
float knotX(int n) {
return sin(radians(n)) + 2*sin(radians(n*2));
}
float knotY(int n) {
return cos(radians(n)) - 2*cos(radians(n*2));
}
float knotZ(int n) {
return sin(radians(n*3))*-1;
}
Wolfram Mathematica has basic knot theory, including visualization, built in:
http://reference.wolfram.com/language/ref/KnotData.html
This webpage from the software documentation contains many examples of standard knot-theoretic visualization and computation that can be done with Mathematica.

Determining whether geographic point is within X meters of a state border (using shapefile for border data)

So I'm writing a Java app, and I've got an ESRI Shapefile which contains the borders of all the U.S. states. What I need is to be able to determine whether any given lat/lon point is within a specified distance from ANY state border line - i.e., I will not be specifying a particular border line, just need to see whether the point is close to any of them.
The solution does NOT have to be very precise at all; e.g. I don't need to be dealing with measuring perpendicular to the border, or whatever. Just checking to see if going X meters north, south, east or west would result in crossing a border would be more than sufficient. The solution DOES have to be computationally efficient, as I'll be performing a huge number of these calculations.
I'm planning to use the GeoTools library (though if there's a simpler option, I'm all for it) with the Shapefile plugin. What I don't really understand is: Once I've got the shapefile loaded into memory, how do I check to see whether I'm near a border?
Thanks!
-Dan
Assuming JTS for Geometry which is what is included in GeoTools:
public boolean pointIsClose( File file, Point targetPoint,double distance) {
boolean ret = false;
Map connect = new HashMap();
connect.put("url", file.toURL());
DataStore dataStore = DataStoreFinder.getDataStore(connect);
FeatureSource featureSource = dataStore.getFeatureSource(typeName);
FeatureCollection collection = featureSource.getFeatures();
FeatureIterator iterator = collection.features();
try {
while (iterator.hasNext()) {
Feature feature = iterator.next();
Geometry sourceGeometry = feature.getDefaultGeometry();
ret= sourceGeometry.isWithinDistance(targetPoint, distance );
}
} finally {
iterator.close();
}
return ret;
}
The double number will have to come from the CRS which will define the units in which the calculation will be performed.
These are the geotools imports:
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.Feature;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
If you just want to know if point A is within X meters of a state border and X is constant and you don't care which border it is, you can precompute the negative space as a series of boxes. Then all you have to do is a contains check for each of those boxes against the point. If none of them match, you're not in the negative space.
If you can somehow extract the shape for each state from the shapefile, create an envelope that is x meters on a side (with your point in the exact center) and see if those two shapes intersect, you'll be able to answer the question.
If I was using ESRI's ArcGIS Engine, I'd use an ISpatialFilter with the point defined in the geometry (possibly with a buffer) and query that against the States shapefile. Any result(s) returned would indicate that the point was near a state. I'm unfamiliar with GeoTools and, while browsing through their documentation, I didn't come across anything that looked like that type of functionality, but they must have it. You may want to look for examples of how to use GeoTools to perform spatial queries on shapefiles.

Categories