Displaying differences for changeset
 
display as  

src/java/m/rse/cfactor/utils/PGTools.java

@@ -229,7 +229,14 @@
 
     private ArrayList<PGTools.PolygonLatLon.PointLatLon> points;
     private Boolean bValid;
+    
+        public enum ESRIContourType {
 
+        outer, inner, unknown
+    };
+        
+        protected ESRIContourType myRotation;    
+        
     public PolygonLatLon() {
         this.bValid = true;
         this.points = new ArrayList<>();
@@ -285,9 +292,68 @@
     }
 
     public Boolean isValid() {
+            this.getRotation();
         return this.bValid;
     }
+        
+        protected void swapRotation() {
+            ArrayList<PointLatLon> newPoints = new ArrayList<>();
+            int j;
 
+            for (j = this.points.size() - 1; j >= 0; j--) {
+                PointLatLon tPoint = new PointLatLon(this.points.get(j).getLat(), this.points.get(j).getLon());
+                newPoints.add(tPoint);
+            }
+
+            this.points.clear();
+            this.points = newPoints;
+        }
+
+        public Boolean makeESRIRotation(ESRIContourType contourType) {
+            Boolean ret_val = false;
+            if (this.isValid()) {
+                if (((contourType == ESRIContourType.outer) && (this.myRotation == ESRIContourType.inner))
+                        || ((contourType == ESRIContourType.inner) && (this.myRotation == ESRIContourType.outer))) {
+                    this.swapRotation();
+                }
+                ret_val = true;
+            }
+
+            return ret_val;
+        }     
+        
+        protected void getRotation() {
+            double contour_area = 0.0;
+            int j;
+            PointLatLon a, b;
+
+            this.myRotation = ESRIContourType.unknown;
+
+            if (this.bValid) {
+
+                //  Calculate cartesian area...test for positive or negative value.  This tells the rotation of points CW versus CCW.
+                //  NOTE:  Don't use this method to calc "real" areas of Lat/Lon!  This is just a fast way of finding out the order of points.
+                //         Lat/Lon must be projected first to get actual area using this type of cross-product method.
+                if (this.points.size() > 2) {
+                    for (j = 0; j < this.points.size() - 2; j++) {
+                        a = this.points.get(j);
+                        b = this.points.get(j + 1);
+
+                        contour_area += a.getLon() * b.getLat() - b.getLon() * a.getLat();
+                    }
+                    a = this.points.get(j);
+                    b = this.points.get(0);
+                    contour_area += a.getLon() * b.getLat() - b.getLon() * a.getLat();
+
+                    if (contour_area < 0) {
+                        this.myRotation = ESRIContourType.inner;  //CCW            
+                    } else {
+                        this.myRotation = ESRIContourType.outer; //CW            
+                    }
+                }
+            }
+        }
+        
     public static class PointLatLon {
 
         private Double Lat;
@@ -308,7 +374,7 @@
         }
         }
 
-        public Boolean isValid() {
+        public Boolean isValid() {                
         return this.bValid;
         }