Yusuf
Yusuf

Reputation: 119

How to use Javas Polygon class with lat,longs

I have two geo-spatial simple convex polygon areas, in latitudes and longitudes (decimal degrees) that I want to compute using Javas Polygon class. I basically want to see if these two lat,long polygons intersect, and to calculate the areas (in sq meters) of both. Now java.awt.Polygon class only uses x,y co-ordinate system, but I need to use lats,longs. How can I do this, and/or is there any other classes/libraries available?

Upvotes: 1

Views: 3519

Answers (5)

speedygonzales
speedygonzales

Reputation: 169

Here is a solution that works like a charm:

Class PolygonArea from net.sf.geographiclib

Refer link below with sample code: https://geographiclib.sourceforge.io/html/java/net/sf/geographiclib/PolygonArea.html

gradle dependency: compile group: 'net.sf.geographiclib', name: 'GeographicLib-Java', version: '1.42'

Upvotes: 0

Filippo Mazza
Filippo Mazza

Reputation: 4367

Why not use Polygon class multiplying coordinates for 10^n making them integers? Polygon accepts arrays of int as points.

Just an Idea

Upvotes: 1

Jack
Jack

Reputation: 1008

Maybe you could use GeoTools for this. It allows you to create Geometry objects, and check wether they intersect (see: Geometry Relationships)

Upvotes: 0

duffymo
duffymo

Reputation: 308743

All you need to do is convert from spherical to rectangular coordinates if you think that really matters.

I'm not sure that it does, because the java.awt.Polygon is merely a data structure holding for pairs of values. If you read the javadocs, they say

The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon. Internally, a polygon comprises of a list of (x,y) coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of (x,y) points are joined by a line segment that closes the polygon.

They happen to label those points as x and y, which makes all of us think rectangular coordinates, but they need not be from your point of view. A bug on the service of your sphere would view it as a 2D space. Your radius is large and constant. You just can't count on using any of Polygon's methods (e.g., contains(), getBounds2D(), etc.)

In this case, the important thing is your area calculation. You can use a Polygon for your area calculation, storing lats and longs, as long as you view Polygon as a data structure.

You might also thing of abandoning this idea and writing your own. It's not too hard to create your own Point and Polygon for spherical coordinates and doing it all with that coordinate system in mind from the start. Better abstraction, less guessing for clients. Your attempt to reuse java.awt.Polygon is admirable, but not necessary.

You can perform the area calculation easily by converting it to a contour integral and using Gaussian quadrature to integrate along each straight line boundary segment. You can even include the curvature of each segment at each integration point, since you know the formula.

Upvotes: 0

CiscoIPPhone
CiscoIPPhone

Reputation: 9477

Going off my intuition here.

If the polygons are small in ratio to the size of the planet you can treat them as flat polygons. The steps involved would be converting the lat/long into absolute x/y/z, taking any three of the points and finding the normal of the plane the polygons lie on and then using this to project the points into two dimensions. Once you have the 2D points it's easy to calculate the area or if they intersect.

Probably not the best answer but hopefully it will motivate some people to make better ones because it's a good question.

Upvotes: 0

Related Questions