Displaying differences for changeset
 
display as  

src/java/lamps/Const.java

@@ -12,6 +12,8 @@
 public class Const {
 
     // georeferencing database, shapfile with States & CMZs
+    public static final String Statesshp = "GIS_analysis_data/USStates.shp";
+    public static final String CMZshp = "GIS_analysis_data/CMZs.shp";
     public static final String StatesCMZshp = "GIS_analysis_data/States_CMZs.shp";
     
     //linkage between NASS GeoTIFFs pixel ID and actual vegetation names/classes

src/java/local/Georeferencing.java

@@ -12,12 +12,13 @@
 import com.vividsolutions.jts.geom.Polygon;
 import java.io.File;
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
 import lamps.Const;
-import static local.Read_shp.featureCollectionFromShapefile;
+import static local.Read_Geometry.featureCollectionFromShapefile;
 import org.geotools.data.DataStore;
 import org.geotools.data.FeatureSource;
 import org.geotools.data.FileDataStore;
@@ -29,7 +30,9 @@
 import org.geotools.feature.FeatureIterator;
 import org.geotools.geometry.jts.JTS;
 import org.geotools.geometry.jts.JTSFactoryFinder;
+import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.referencing.CRS;
+import org.geotools.resources.coverage.IntersectUtils;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.type.FeatureType;
 import org.opengis.geometry.MismatchedDimensionException;
@@ -58,6 +61,7 @@
             SimpleFeature feature = featureIterator.next();
             MultiPolygon mp = (MultiPolygon) feature.getDefaultGeometry();
             int n = mp.getNumGeometries();
+            System.out.println(" number of geometries:" + n);
             for (int i = 0; i < n; i++) {
                 Polygon poly = (Polygon) mp.getGeometryN(i);
                 for (int j = 0; j < hru_cent_point_coord.size(); j++) {
@@ -89,7 +93,7 @@
         ArrayList<String[]> back = new ArrayList<>();
 
         File Statesfile = new File(dataDir, Const.StatesCMZshp);
-        
+
         URL dir_url = Statesfile.toURI().toURL();
         FeatureCollection featureCollection_States = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url);
         SimpleFeatureIterator featureIterator = (SimpleFeatureIterator) featureCollection_States.features();
@@ -97,6 +101,7 @@
         while (featureIterator.hasNext()) {
             SimpleFeature feature = featureIterator.next();
             MultiPolygon mp = (MultiPolygon) feature.getDefaultGeometry();
+            //Geometry intersection = mp.intersection(JTS.toGeometry(bounds));
             int n = mp.getNumGeometries();
             for (int i = 0; i < n; i++) {
                 Polygon poly = (Polygon) mp.getGeometryN(i);
@@ -129,6 +134,106 @@
         return back;
     }
 
+    public static ArrayList<String[]> States_CMS_intersect_fast(File dataDir, CoordinateReferenceSystem srcCRS, ReferencedEnvelope env, List<Geometry> AOI, PrintWriter w_log) throws FactoryException, URISyntaxException, IOException, MismatchedDimensionException, TransformException {
+
+        CoordinateReferenceSystem targetCRS = CRS.parseWKT(Const.WKT_CooSyst);
+        //MathTransform transform = CRS.findMathTransform(srcCRS, targetCRS, true);
+
+        ArrayList<String[]> back = new ArrayList<>();
+
+        File Statesfile = new File(dataDir, Const.Statesshp);
+
+        URL dir_url = Statesfile.toURI().toURL();
+        FeatureCollection featureCollection_States = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url);
+        SimpleFeatureIterator featureIterator = (SimpleFeatureIterator) featureCollection_States.features();
+
+        List<Geometry> allStates = new ArrayList<>();
+        String States = "";
+
+        while (featureIterator.hasNext()) {
+            SimpleFeature feature = featureIterator.next();
+            Geometry geometry = (Geometry) feature.getDefaultGeometry();
+            Geometry intersection = geometry.intersection(JTS.toGeometry(env));
+
+            if (intersection instanceof MultiPolygon) {
+                MultiPolygon mp = (MultiPolygon) intersection;
+                for (int i = 0; i < mp.getNumGeometries(); i++) {
+                    com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(i);
+                    Geometry gIntersection = IntersectUtils.intersection(g, JTS.toGeometry(env));
+                    States = States + (String) feature.getProperty("STATE").getValue() + ";";
+                    allStates.add(gIntersection);
+                }
+            } else if (intersection instanceof Polygon) {
+                com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection;
+                Geometry gIntersection = IntersectUtils.intersection(g, JTS.toGeometry(env));
+                States = States + (String) feature.getProperty("STATE").getValue() + ";";
+                allStates.add(gIntersection);
+            }
+        }
+        featureIterator.close();
+
+        w_log.println(States);
+        w_log.flush();
+
+        if (allStates.size() > 1) {
+            w_log.println("State size " + allStates.size());
+            w_log.println(States);
+            w_log.flush();
+
+            // Todo a lot of code ;-)
+        }
+
+        File CMZfile = new File(dataDir, Const.CMZshp);
+
+        dir_url = CMZfile.toURI().toURL();
+        featureCollection_States = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url);
+        featureIterator = (SimpleFeatureIterator) featureCollection_States.features();
+
+        List<Geometry> all_CMZs = new ArrayList<>();
+        String CMZs = "";
+
+        while (featureIterator.hasNext()) {
+            SimpleFeature feature = featureIterator.next();
+            Geometry geometry = (Geometry) feature.getDefaultGeometry();
+            Geometry intersection = geometry.intersection(JTS.toGeometry(env));
+
+            if (intersection instanceof MultiPolygon) {
+                MultiPolygon mp = (MultiPolygon) intersection;
+                for (int i = 0; i < mp.getNumGeometries(); i++) {
+                    com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(i);
+                    Geometry gIntersection = IntersectUtils.intersection(g, JTS.toGeometry(env));
+                    CMZs = CMZs + (String) feature.getProperty("CMZ").getValue() + ";";
+                    all_CMZs.add(gIntersection);
+                }
+            } else if (intersection instanceof Polygon) {
+                com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection;
+                Geometry gIntersection = IntersectUtils.intersection(g, JTS.toGeometry(env));
+                CMZs = CMZs + (String) feature.getProperty("CMZ").getValue() + ";";
+                all_CMZs.add(gIntersection);
+            }
+        }
+        featureIterator.close();
+
+        w_log.println(CMZs);
+        w_log.flush();
+
+        if (all_CMZs.size() > 1) {
+            w_log.println("CMZ size " + all_CMZs.size());
+            w_log.println(CMZs);
+            w_log.flush();
+
+            // Todo a lot of code ;-)
+        }
+
+        if (allStates.size() == 1 && all_CMZs.size() == 1) {
+            for (int i = 0; i < AOI.size(); i++) {
+                back.add(new String[]{i + "", States.replace(";", "") + "&" + CMZs.replace(";", "")});
+            }
+        }
+
+        return back;
+    }
+
     protected static FeatureCollection featureCollectionFromShapefile(URL shapefile) throws IOException {
         DataStore dataStore = new ShapefileDataStore(shapefile);
         FeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);

src/java/local/NASS_GeoTIFF.java

@@ -193,15 +193,16 @@
             }
         }
         Collections.sort(years);
+        
+        String[] back = new String[years.size()];
+        
         w_log.print(" Available NASS CDL year(s): ");
+        
         for (int i = 0; i < years.size(); i++) {
+            back[i] = years.get(i).toString();
             w_log.print(" " + years.get(i));
         }
-        w_log.println(" ");
-        String[] back = new String[years.size()];
-        for (int i = 0; i < years.size(); i++) {
-            back[i] = years.get(i).toString();
-        }
+        
         w_log.println(" ");
         return back;
     }
@@ -326,7 +327,7 @@
                                 if (map.put(oldname, list) != null) {
                                     throw new IllegalArgumentException("Duplicate name: " + oldname);
                                 }
-                                list = new ArrayList<Nass_accurcies_object>();
+                                list = new ArrayList<>();
                                 oldname = in_name;
                             }
                             list.add(new Nass_accurcies_object(state, nass_accuracy, nass_accu_years));
@@ -341,22 +342,13 @@
         }
     }
 
-    public static void NASSIrrigation_clip_analysis(File dataDir, String[] years, String shp_Extend, File workingDir, URL hru_url, List<ERU> hrus, List<String> unique_states, PrintWriter w_log) throws IOException, IllegalAccessException, ParserConfigurationException, SAXException, FactoryException, URISyntaxException, MismatchedDimensionException, TransformException {
+    public static void NASSIrrigation_clip_analysis(File dataDir, String[] years, String shp_Extend, List<Geometry> Input_Geometries, List<ERU> hrus, List<String> unique_states, PrintWriter w_log) throws IOException, IllegalAccessException, ParserConfigurationException, SAXException, FactoryException, URISyntaxException, MismatchedDimensionException, TransformException {
 
         for (int i = 0; i < hrus.size(); i++) {
             hrus.get(i).irrigated = false;
             hrus.get(i).irrigated_area = new double[]{0.0, 0.0};
         }
 
-        CoordinateReferenceSystem targetCRS = CRS.parseWKT(Const.WKT_CooSyst);
-        File dir = new File(hru_url.toURI());
-
-        FileDataStore dataStore = FileDataStoreFinder.getDataStore(dir);
-        FeatureSource shapefileSource_cmz = dataStore.getFeatureSource();
-        FeatureType schema = shapefileSource_cmz.getSchema();
-        CoordinateReferenceSystem hru_sourceCRS = schema.getCoordinateReferenceSystem();
-
-        MathTransform transform = CRS.findMathTransform(hru_sourceCRS, targetCRS, true);
         GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
 
         ArrayList<String[]> nass_id_nm = new ArrayList<String[]>();
@@ -373,8 +365,6 @@
             String ye = (i > 0) ? "2007" : "2002";
             w_log.println("  year " + ye);
 
-            FeatureCollection featureCollection_hru = (FeatureCollection) featureCollectionFromShapefile(hru_url);
-            SimpleFeatureIterator iterator_hru = (SimpleFeatureIterator) featureCollection_hru.features();
             AbstractGridCoverage2DReader reader;
             AbstractGridFormat formatsaga = GridFormatFinder.findFormat(geotiff[i]);
             reader = formatsaga.getReader(geotiff[i].toURI().toURL());
@@ -394,13 +384,11 @@
             hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs);
 
             int y = 0;
-            while (iterator_hru.hasNext()) {
-                SimpleFeature feature = iterator_hru.next();
+            for (int j = 0; j < Input_Geometries.size(); j++) {
 
                 LinearRing lr = null;
-                //SimpleFeature feature = iterator_hru.next();
-                Geometry geometry = (Geometry) feature.getDefaultGeometry();
-                Geometry targetGeometry = JTS.transform(geometry, transform);
+
+                Geometry targetGeometry = Input_Geometries.get(j);
 
                 if (!targetGeometry.isValid()) {
                     targetGeometry = targetGeometry.convexHull();
@@ -512,17 +500,8 @@
     }
 
     public static void NASSGeoTIFF_clip_analysis(File dataDir, String[] years, String shp_Extend, File outputDir,
-            URL hru_url, List<ERU> hrus, List<String> unique_states, PrintWriter w_log) throws IOException, IllegalAccessException, ParserConfigurationException, SAXException, FactoryException, URISyntaxException, MismatchedDimensionException, TransformException {
+            List<Geometry> Input_Geometries, List<ERU> hrus, List<String> unique_states, PrintWriter w_log) throws IOException, IllegalAccessException, ParserConfigurationException, SAXException, FactoryException, URISyntaxException, MismatchedDimensionException, TransformException {
 
-        CoordinateReferenceSystem targetCRS = CRS.parseWKT(Const.WKT_CooSyst);
-        File dir = new File(hru_url.toURI());
-
-        FileDataStore dataStore = FileDataStoreFinder.getDataStore(dir);
-        FeatureSource shapefileSource_cmz = dataStore.getFeatureSource();
-        FeatureType schema = shapefileSource_cmz.getSchema();
-        CoordinateReferenceSystem hru_sourceCRS = schema.getCoordinateReferenceSystem();
-
-        MathTransform transform = CRS.findMathTransform(hru_sourceCRS, targetCRS, true);
         GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);
 
         ArrayList<String[]> nass_id_nm = new ArrayList<String[]>();
@@ -538,9 +517,6 @@
         for (int i = 0; i < years.length; i++) {
             //System.out.println("  year " + years[i]);
 
-            FeatureCollection featureCollection_hru = (FeatureCollection) featureCollectionFromShapefile(hru_url);
-            SimpleFeatureIterator iterator_hru = (SimpleFeatureIterator) featureCollection_hru.features();
-
             AbstractGridCoverage2DReader reader;
             AbstractGridFormat format = GridFormatFinder.findFormat(new File(outputDir, "NASS_" + years[i] + ".tif"));
             reader = format.getReader(new File(outputDir, "NASS_" + years[i] + ".tif"));
@@ -574,14 +550,11 @@
 //            }
 //            System.out.println(" full no data size : "+ nodata.size());
             int y = 0;
-            while (iterator_hru.hasNext()) {
-
-                SimpleFeature feature = iterator_hru.next();
+           for (int bj = 0; bj < Input_Geometries.size(); bj++) {
 
                 LinearRing lr = null;
-                //SimpleFeature feature = iterator_hru.next();
-                Geometry geometry = (Geometry) feature.getDefaultGeometry();
-                Geometry targetGeometry = JTS.transform(geometry, transform);
+
+                Geometry targetGeometry = Input_Geometries.get(bj);
 
                 if (!targetGeometry.isValid()) {
                     targetGeometry = targetGeometry.convexHull();

src/java/local/NASS_LMOD_Matching.java

@@ -201,7 +201,7 @@
         }
     }
 
-    public static void NASS_LMOD_data_writer(File outputDir, File dataDir, List<ERU> hrus, PrintWriter w_log) throws IOException, URISyntaxException, SQLException, ClassNotFoundException {
+    public static void NASS_LMOD_data_writer(File workspace, File dataDir, List<ERU> hrus, PrintWriter w_log) throws IOException, URISyntaxException, SQLException, ClassNotFoundException {
 
         ArrayList<String> Unique_LMOD_KEYs = new ArrayList<>();
         for (int i = 0; i < hrus.size(); i++) {
@@ -251,7 +251,7 @@
 
         }
 
-        PrintWriter w = new PrintWriter(new FileWriter(new File(outputDir, Const.AOI_NASS_LMOD_result)));
+        PrintWriter w = new PrintWriter(new FileWriter(new File(workspace, Const.AOI_NASS_LMOD_result)));
 
         w.println("PolygonID, NASS-Year, Date, Seed/Harvest, Vegetation ");
 

src/java/local/objects/ERU.java

@@ -15,7 +15,6 @@
 public class ERU {
 
     public int ID;
-    public URL url;
     public ArrayList<String> state;
     public ArrayList<String> cmz;
     public Boolean irrigated;

src/java/m/lamps/V1_0.java

@@ -4,6 +4,7 @@
  */
 package m.lamps;
 
+import com.vividsolutions.jts.geom.Geometry;
 import java.util.Map;
 import java.util.concurrent.Callable;
 import javax.ws.rs.Path;
@@ -92,7 +93,8 @@
     private void run(String geometry, File outputDir, File dataDir, File workspace) throws Exception {
 
         outputDir.mkdirs();
-
+        
+        List<Geometry> Input_Geometries = new ArrayList<>();
         List<ERU> hrus = new ArrayList<>();
         List<String> unique_states = new ArrayList<>();
         List<String> unique_cmzs = new ArrayList<>();
@@ -106,8 +108,7 @@
         w_log.println("===>  Reading AOI");
         w_log.flush();
         //String shp_Extend = Read_Geometry.URL_Read_Geometry_File(dataDir, geomUrl, workspace, hrus, unique_states, unique_cmzs, w_log);
-        String shp_Extend = Read_Geometry.URL_Read_Geometry_File_new(dataDir, geomUrl, workspace, hrus, unique_states, unique_cmzs, w_log);
-        geomUrl = hrus.get(0).url;
+        String shp_Extend = Read_Geometry.URL_Read_Geometry_File_new(dataDir, geomUrl, Input_Geometries, workspace, hrus, unique_states, unique_cmzs, w_log);
         w_log.println("===>  URL: " + geomUrl.toString());
         w_log.flush();
         w_log.println("===>  Check available NASS years.");
@@ -122,11 +123,11 @@
 
         w_log.println("===>  Irrigation clipping.");
         w_log.flush();
-        NASS_GeoTIFF.NASSIrrigation_clip_analysis(dataDir, years_CDL, shp_Extend, geomUrl.toURI().toURL(), hrus, unique_states, w_log);
+        NASS_GeoTIFF.NASSIrrigation_clip_analysis(dataDir, years_CDL, shp_Extend, Input_Geometries, hrus, unique_states, w_log);
 
         w_log.println("===>  NASS clipping.");
         w_log.flush();
-        NASS_GeoTIFF.NASSGeoTIFF_clip_analysis(dataDir, years_CDL, shp_Extend, outputDir, geomUrl.toURI().toURL(), hrus, unique_states, w_log);
+        NASS_GeoTIFF.NASSGeoTIFF_clip_analysis(dataDir, years_CDL, shp_Extend, outputDir, Input_Geometries, hrus, unique_states, w_log);
 
         w_log.println("===>  Output generation.");
         w_log.flush();