I'm looking for a java library for android or an algorithm description which can calculate the difference between 2 polygons. I've been looking for a few hours with no luck. Any suggestions?
Check out the JTS library. It gets tons of use in the Java GIS world, and is generally useful for solving topology problems in Java. JTS represents various topology types (including Polygons) as with a Geometry base class. Here's the method for calculating the difference between two geometries (including two polygons): http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html#difference%28com.vividsolutions.jts.geom.Geometry%29
You can download JTS from http://sourceforge.net/projects/jts-topo-suite/.
The subtraction?
Check out the Area API. It has a subtract() method.
Related
I have a requirement to process geojson file (it has multiple polygons) and find a given point (longitude and latitude) contains with in polygon. I am looking for a java solution. Can you please recommend possible solutions?
Thanks.
If you need more functionality than what JTS offers, you should check out GeoTools.
It provides the ability to read and write most major cartographic formats, supports map projections and coordinate transformations, and is a much more full featured GIS suite.
JTS is strictly geometry -- it deals with 2d shapes with no units attached.
When one needs a geometry library for Java JTS should be mentioned at first. And of course Java-GeoJSon to load the data. The latter library is built upon JTS, so you even don't need any special adapters between two libraries.
I have a program that allows the user to draw vertices and edges on a JFrame of size 1000 by 750. Now I need to use a quadtree to partition the input graph depending on how many vertices there are in a single quadrant. I would really appreciate it if someone can point me in the right direction on how to achieve this?
Additional information:
I have an Edge class which stores: source (vertex), target (vertex) and weight.
I have a Vertex class which stores: name, x-coordinates, y-coordinates, and Edge[] adjacentList.
I also have a Graph class which stores two ArrayLists: edges and vertices.
I've recently implemented code, that should solve your problem. It's free for download on my recent blog post. Quadtrees for Space Decomposition, Java Implementation http://kirstywilliams.co.uk/blog/2012/08/quadtrees-java-implementation/
Some more quadtree implementations which are both apache licensed:
Apache SIS - no tests, fast, but several improvements necessary
GraphHopper - with tests, less memory consumption but probably a bit slower. The distance calculation is pluggable.
But if you already have a connected graph in memory you can create a much more memory efficient (but also a bit complicated) solution. Parts are implemented here, more details available here.
Another interesting repo (but AGPL I think as neo4j itself) with some spatial collections.
I need to implement a hierarchical clustering algorithm based on a custom distance. The distance is computed by looking in a database for the value associated to the two ids of the objects that are being compared.
Is there an easy way to do this in Java? I took a look at Weka and their custom distance function but I cannot find a way to define instances so that when I am in the custom distance function I can get the IDs of the two original objects.
Any help would be greatly appreciated
Thanks a lot in advance
Rossella
You can take a look on Apache Mahout.
Here is a link Mahout Hierarchical clustering
This tool is written in Java and its open source.
I'm hoping for the equivalent of CGAL (in C++)--I want convex partitioning of polygons or at least triangulation. It also has to be free. A previous question suggested JTS, but it doesn't seem to have those functions.
JTS supports delaunay triangulation, but not constrained triangulation (holes):
http://lin-ear-th-inking.blogspot.com/2009/04/delaunay-triangulation-in-jts-111.html
For constrained delaunay triangulation you can use Poly2Tri.
Works quite good, but could not triangulate polygons with holes, where the holes touch the outline or touch another polygon.
At the moment I use the OpenGl Tesselator (glut) with the java wrapper jogl (http://jogamp.org/) for triangulation in java.
You have to implement the correct callbacks and do some fiddling in there.
Neither of them satisfies all (my) needs, but until now I haven't found a better one.
GeoTools has a pretty extensive library of geometry manipulation algorithms, but of course just like JTS it is somewhat GIS-oriented.
http://www.geotools.org/
CGAL has SWIG driven bindings since 2012. 2D conforming triangulation and meshes is part of it. Please visit the github wiki for furhter info's.
You could try GeoLib which is an excellent geometry package.
I'm trying to create a method that will take in two arbitrary lists of nodes, for a subject and a clipping polygon, and output either:
a) the area of the overlap
b) a list of nodes for the resulting (clipped) polygon so that I can calculate the area
I've found lots of examples which clip an arbitrary polygon using a rectangular window (which is fairly standard in graphics) but that's not what I need. I understand that it's fairly complex, particularly when you get holes, convex polygons and the like. The only simplifying assumption which I can make is that the arbitrary polygons will not contain any holes.
I'm not at all an expert in this field, so would something like the Sutherland-Hodgman algorithm work? Are there any libraries out there which already do this, or is my best bet to simply implement the algorithm as described in pseudo-code on Wikipedia?
Thanks for the help!
Are there any libraries out there which already do this ...
Polygon clipping is a complex task. I wouldn't recommend trying to do it yourself unless you want to spend many months on it.
Wikipedia lists a number of clipping libraries (and IIRC in that list only Clipper is free for use in commercial applications):
http://en.wikipedia.org/wiki/Boolean_operations_on_polygons#External_links
ps: I admit to a personal bias for Clipper since I'm the author :)
More info here: http://angusj.com/delphi/clipper.php
Sounds like Weiler-Atherton is the one you need:
The algorithm requires polygons to be
clockwise and not reentrant (self
intersecting). The algorithm can
support holes (as counter-clockwise
polygons wholly inside their parent
polygon), but requires additional
algorithms to decide which polygons
are holes.
Your polygons fit those criteria, right?
I don't know about implementations, but it sounds like you would be better off implementing W-A than S-H if either of your polygons could be concave.
Try gpc.
I found that using the JavaGeom library worked very well. It integrates the code from the Java port of GPC (which is no longer available) and thus allows arbitrary polygon operations. Using SimplePolygon2D and Polygon2DUtils.intersection() I was able to get the desired operation.
I've tried a lot of different libraries and the one that worked best was the JTS Topological Suite which is pure Java and LGPL2 licensed.