tags:

views:

357

answers:

3

Hi,

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?

A: 

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.

CiscoIPPhone
A: 

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.

duffymo
If lat/long (or if coordinates returned from the spherical to rectangular) were stored in the Polygon, calculating the area would fail sometimes due to the variables looping.
CiscoIPPhone
I'm not following you here. Polygon is nothing more than a data structure for two arrays of values. The appropriate storage of values and success or failure of the area calculation has nothing to do with using java.awt.Polygon. You can make the same mistakes if you roll your own class.
duffymo
thanks duffymo,My problem is simplified by the fact that the height is always zero, i.e. the polygons are on the earths surface.I just need to figure out how represent a latitute or longitude as a spherical coordinate, then i can convert it to rectangular.You do make a good point about rather rolling my own (or using something) instead of using ava.awt.Polygon
Yusuf
The usual convention when somebody wants to store verticies of a shape is to start with an arbitrary one and add the rest in an order that preserves a walk around the perimeter in a consistent direction. If you had a rectangular polygon, you'd have four points numbered in a clockwise or anti-clockwise walk.
duffymo
The height isn't "zero". CiscoIPPhone had it right - the curvature effect is not significant for you because the radius of the earth is so large compared to the dimensions of your polygon. Small and neglectable, but not zero.
duffymo
A: 

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

Jack
@Jack: 404 not found.
Dave Jarvis
Fixed, thanks Dave!
Jack