@@ -10,16 +10,19 @@ |
import java.io.File; |
import java.io.IOException; |
import java.io.PrintWriter; |
-import java.net.URISyntaxException; |
+import java.net.MalformedURLException; |
import java.net.URL; |
import java.util.ArrayList; |
-import java.util.Arrays; |
import java.util.List; |
+import java.util.concurrent.Callable; |
+import java.util.concurrent.ExecutionException; |
+import java.util.concurrent.ExecutorService; |
+import java.util.concurrent.Executors; |
+import java.util.concurrent.FutureTask; |
import lamps.CSIP_Const; |
import methods.objects.ERU; |
import org.geotools.data.DataStore; |
import org.geotools.data.FeatureSource; |
-import org.geotools.data.collection.ListFeatureCollection; |
import org.geotools.data.shapefile.ShapefileDataStore; |
import org.geotools.data.simple.SimpleFeatureCollection; |
import org.geotools.data.simple.SimpleFeatureIterator; |
@@ -28,11 +31,6 @@ |
import org.geotools.geometry.jts.ReferencedEnvelope; |
import org.geotools.resources.coverage.IntersectUtils; |
import org.opengis.feature.simple.SimpleFeature; |
-import org.opengis.feature.simple.SimpleFeatureType; |
-import org.opengis.geometry.MismatchedDimensionException; |
-import org.opengis.referencing.FactoryException; |
-import org.opengis.referencing.crs.CoordinateReferenceSystem; |
-import org.opengis.referencing.operation.TransformException; |
|
/** |
* |
@@ -40,35 +38,55 @@ |
*/ |
public class CSIP_Georeferencing { |
|
- public static ArrayList<ArrayList<String>> States_CMS_intersect_fast(File dataDir, CoordinateReferenceSystem srcCRS, ReferencedEnvelope env, List<Geometry> AOI, List<ERU> hrus, List<String> CDL_years, PrintWriter w_log) throws FactoryException, URISyntaxException, IOException, MismatchedDimensionException, TransformException, Exception { |
- |
- ArrayList<ArrayList<String>> back = new ArrayList<>(); |
- |
- boolean all_done = false; |
+ /** |
+ * |
+ * @param dataDir |
+ * @param env |
+ * @param AOI |
+ * @param multigeo |
+ * @param w_log |
+ * @return |
+ * @throws MalformedURLException |
+ * @throws IOException |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
+ public static List<ERU> States_CMZs_intersect_fast(File dataDir, ReferencedEnvelope env, List<Geometry> AOI, Geometry multigeo, PrintWriter w_log) throws MalformedURLException, IOException, InterruptedException, ExecutionException { |
+ List<ERU> local_hrus = new ArrayList<>(); |
|
File Statesfile = new File(dataDir, CSIP_Const.Statesshp); |
|
URL dir_url = Statesfile.toURI().toURL(); |
- FeatureCollection featureCollection_States = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url); |
+ FeatureCollection featureCollection = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url); |
|
- SimpleFeatureIterator featureIterator = (SimpleFeatureIterator) featureCollection_States.features(); |
+ SimpleFeatureIterator featureIterator = (SimpleFeatureIterator) featureCollection.features(); |
|
ArrayList<String> states = new ArrayList<>(); |
- ArrayList<SimpleFeature> list = new ArrayList<>(); |
- SimpleFeatureType simp_type = null; |
+ ArrayList<String> CMZs = new ArrayList<>(); |
|
+ ArrayList<SimpleFeature> geo_list_states = new ArrayList<>(); |
+ ArrayList<SimpleFeature> geo_list_cmzs = new ArrayList<>(); |
+ //SimpleFeatureType simp_type = featureIterator.next().getType(); |
+ |
+ Geometry extent = JTS.toGeometry(env); |
+ if (multigeo != null) { |
+ extent = multigeo; |
+ } |
while (featureIterator.hasNext()) { |
SimpleFeature feature = featureIterator.next(); |
- simp_type = feature.getType(); |
+ //simp_type = feature.getType(); |
Geometry geometry = (Geometry) feature.getDefaultGeometry(); |
|
- if ((JTS.toGeometry(env)).within(geometry)) { |
+ if (extent.within(geometry)) { |
states.add(feature.getProperty("STATE").getValue().toString()); |
+ if (!geo_list_states.contains(feature)) { |
+ geo_list_states.add(feature); |
+ } |
break; |
|
- } else if ((JTS.toGeometry(env)).intersects(geometry)) { |
+ } else if (extent.intersects(geometry)) { |
|
- Geometry intersection = geometry.intersection(JTS.toGeometry(env)); |
+ Geometry intersection = geometry.intersection(extent); |
|
if (intersection instanceof MultiPolygon) { |
MultiPolygon mp = (MultiPolygon) intersection; |
@@ -78,8 +96,8 @@ |
if (!states.contains(feature.getProperty("STATE").getValue().toString())) { |
states.add(feature.getProperty("STATE").getValue().toString()); |
} |
- if (!list.contains(feature)) { |
- list.add(feature); |
+ if (!geo_list_states.contains(feature)) { |
+ geo_list_states.add(feature); |
} |
//all_States_parts.union(gIntersection); |
} |
@@ -89,147 +107,297 @@ |
if (!states.contains(feature.getProperty("STATE").getValue().toString())) { |
states.add(feature.getProperty("STATE").getValue().toString()); |
} |
- if (!list.contains(feature)) { |
- list.add(feature); |
+ if (!geo_list_states.contains(feature)) { |
+ geo_list_states.add(feature); |
} |
//all_States_parts.union(gIntersection); |
} |
} |
} |
+ w_log.println("===> Done rough State check. " + states.size()); |
+ for (int i = 0; i < states.size(); i++) { |
+ w_log.println(i + " " + states.get(i)); |
+ } |
+ w_log.flush(); |
+ |
featureIterator.close(); |
- SimpleFeatureCollection collection = new ListFeatureCollection(simp_type, list); |
|
- if (states.size() > 1) { |
- CDL_years.addAll(Arrays.asList(CSIP_Const.ACC_years)); |
- } |
- // only one state |
- |
- // done with state overlay !!!!!!! |
- // ============== CMZs ====================================================================== |
- list.clear(); |
File CMZfile = new File(dataDir, CSIP_Const.CMZshp); |
|
dir_url = CMZfile.toURI().toURL(); |
- featureCollection_States = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url); |
- featureIterator = (SimpleFeatureIterator) featureCollection_States.features(); |
+ featureCollection = (SimpleFeatureCollection) featureCollectionFromShapefile(dir_url); |
+ featureIterator = (SimpleFeatureIterator) featureCollection.features(); |
|
//List<Geometry> all_CMZs = new ArrayList<>(); |
- ArrayList<String> CMZs = new ArrayList<>(); |
- |
while (featureIterator.hasNext()) { |
SimpleFeature feature = featureIterator.next(); |
- simp_type = feature.getType(); |
+ //simp_type = feature.getType(); |
Geometry geometry = (Geometry) feature.getDefaultGeometry(); |
|
- if ((JTS.toGeometry(env)).within(geometry)) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
+ if (extent.within(geometry)) { |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
+ } |
+ CMZs.add(no_start_zero); |
+ if (!geo_list_cmzs.contains(feature)) { |
+ geo_list_cmzs.add(feature); |
+ } |
break; |
|
- } else if ((JTS.toGeometry(env)).intersects(geometry)) { |
- Geometry intersection = geometry.intersection(JTS.toGeometry(env)); |
+ } else if (extent.intersects(geometry)) { |
+ Geometry intersection = geometry.intersection(extent); |
|
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)); |
- if (!CMZs.contains(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""))) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
- } |
- //all_CMZs.add(gIntersection); |
- if (!list.contains(feature)) { |
- list.add(feature); |
- } |
+// MultiPolygon mp = (MultiPolygon) intersection; |
+// double intersection_area_portion = 0; |
+// 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)); |
+// intersection_area_portion = (gIntersection.getArea() / AOI.get(i).getArea()) * 100; |
+// } |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
} |
- } 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)); |
- if (!CMZs.contains(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""))) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
+ if (!CMZs.contains(no_start_zero)) { |
+ CMZs.add(no_start_zero); |
} |
//all_CMZs.add(gIntersection); |
- if (!list.contains(feature)) { |
- list.add(feature); |
+ if (!geo_list_cmzs.contains(feature)) { |
+ geo_list_cmzs.add(feature); |
+ } |
+ |
+ } 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)); |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
+ } |
+ if (!CMZs.contains(no_start_zero)) { |
+ CMZs.add(no_start_zero); |
+ } |
+ //all_CMZs.add(gIntersection); |
+ if (!geo_list_cmzs.contains(feature)) { |
+ geo_list_cmzs.add(feature); |
} |
} |
} |
} |
featureIterator.close(); |
- collection = new ListFeatureCollection(simp_type, list); |
|
- back.add(CMZs); |
- back.add(states); |
+ w_log.println("===> Done rough CMZ check. " + CMZs.size()); |
+ for (int i = 0; i < CMZs.size(); i++) { |
+ w_log.println(i + " " + CMZs.get(i)); |
+ } |
+ w_log.flush(); |
|
- if (CMZs.size() > 1) { |
- |
- for (int i = 0; i < AOI.size(); i++) { |
- |
- CMZs = new ArrayList<>(); |
- |
- featureIterator = collection.features(); |
- |
- while (featureIterator.hasNext()) { |
- |
- SimpleFeature feature = featureIterator.next(); |
- |
- Geometry geometry = (Geometry) feature.getDefaultGeometry(); |
- |
- if (AOI.get(i).within(geometry)) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
- |
- } else if (AOI.get(i).intersects(geometry) || AOI.get(i).crosses(geometry)) { |
- |
- Geometry intersection = AOI.get(i).intersection(geometry); |
- if (intersection instanceof MultiPolygon) { |
- MultiPolygon mp = (MultiPolygon) intersection; |
- double sum_area_part = 0; |
- for (int uu = 0; uu < mp.getNumGeometries(); uu++) { |
- com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(uu); |
- Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
- sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.get(i).getArea()) * 100); |
- if (!CMZs.contains(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")) && sum_area_part > 30) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
- } |
- } |
- } |
- if (intersection instanceof Polygon) { |
- com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection; |
- Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
- double intersection_area_portion = (gIntersection.getArea() / AOI.get(i).getArea()) * 100; |
- |
- if (!CMZs.contains(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")) && intersection_area_portion > 30) { |
- CMZs.add(feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "")); |
- } |
- } |
- |
- } |
- |
- } |
- |
- ERU new_hru = new ERU(); |
- new_hru.ID = i; |
- new_hru.state = states; |
- new_hru.cmz = CMZs; |
- new_hru.irrigated = false; |
- new_hru.irrigated_area = 0.0; |
- hrus.add(new_hru); |
- } |
- all_done = true; |
- } |
- |
- if (!all_done) { |
- |
+ if (CMZs.size() < 2 && states.size() < 2) { |
for (int i = 0; i < AOI.size(); i++) { |
ERU new_hru = new ERU(); |
new_hru.ID = i; |
new_hru.state = states; |
new_hru.cmz = CMZs; |
+ new_hru.multiCMZs = false; |
+ new_hru.multiStates = false; |
+ new_hru.geom = AOI.get(i); |
new_hru.irrigated = false; |
new_hru.irrigated_area = 0.0; |
- hrus.add(new_hru); |
+ local_hrus.add(new_hru); |
+ } |
+ |
+ } else { |
+ local_hrus = ConcurrentOverlay(AOI, geo_list_cmzs, geo_list_states, w_log); |
+ } |
+ |
+ w_log.println("===> Done State & CMZ check for all " + local_hrus.size()); |
+ w_log.flush(); |
+ return local_hrus; |
+ } |
+ |
+ /** |
+ * |
+ * @param AOI |
+ * @param list_cmzs |
+ * @param list_states |
+ * @param w_log |
+ * @return |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
+ public static List<ERU> ConcurrentOverlay(List<Geometry> AOI, ArrayList<SimpleFeature> list_cmzs, ArrayList<SimpleFeature> list_states, PrintWriter w_log) throws InterruptedException, ExecutionException { |
+ |
+ int threadnr = 1; |
+ if (AOI.size() > 10) { |
+ threadnr = CSIP_Const.ThreadNumber; |
+ } |
+ final int threadNum = threadnr; |
+ final int first = 0; |
+ final int last = AOI.size() - 1; |
+ final List<Geometry> AOI_fin = AOI; |
+ final ArrayList<SimpleFeature> fin_coll_states = list_states; |
+ final ArrayList<SimpleFeature> fin_coll_cmz = list_cmzs; |
+ final PrintWriter fPW = w_log; |
+ |
+ final int p = (last + 1) / threadNum; |
+ int s = (last + 1) - p * threadNum; |
+ List<ERU> allHRUs = new ArrayList(); |
+ |
+ // Prepare to execute and store the Futures |
+ ExecutorService executor = Executors.newFixedThreadPool(threadNum); |
+ List<FutureTask<List<ERU>>> taskList = new ArrayList<>(); |
+ for (int j = 0; j < threadNum; j++) { |
+ final int ii = j; |
+ FutureTask<List<ERU>> futureTask_1 = new FutureTask<>(new Callable<List<ERU>>() { |
+ int inlast = (ii == threadNum - 1 ? last : p + (ii * p)); |
+ int infirst = (ii == 0 ? first : (p * ii) + 1); |
+ |
+ @Override |
+ public List<ERU> call() throws Exception { |
+ return CSIP_Georeferencing.Overlay(infirst, inlast, AOI_fin, fin_coll_cmz, fin_coll_states, ii, fPW); |
+ } |
+ }); |
+ taskList.add(futureTask_1); |
+ executor.execute(futureTask_1); |
+ } |
+ |
+ // Wait until all results are available and combine them at the same time |
+ for (int j = 0; j < threadNum; j++) { |
+ FutureTask<List<ERU>> futureTask = taskList.get(j); |
+ for (int i = 0; i < futureTask.get().size(); i++) { |
+ allHRUs.add(futureTask.get().get(i)); |
} |
} |
- return back; |
+ executor.shutdown(); |
+ return allHRUs; |
+ } |
+ |
+ /** |
+ * |
+ * @param first |
+ * @param last |
+ * @param AOI |
+ * @param list_cmzs |
+ * @param list_states |
+ * @param part |
+ * @param w_log |
+ * @return |
+ */ |
+ public static List<ERU> Overlay(int first, int last, List<Geometry> AOI, ArrayList<SimpleFeature> list_cmzs, ArrayList<SimpleFeature> list_states, int part, PrintWriter w_log) { |
+ |
+ int count = 0; |
+ |
+ List<ERU> allHRUs = new ArrayList(); |
+ |
+ int part_all = last - first; |
+ |
+ for (int i = first; i <= last; i++) { |
+ |
+ ArrayList<String> CMZs = new ArrayList<>(); |
+ ArrayList<String> states = new ArrayList<>(); |
+ |
+ count += 1; |
+ if (count == 1000) { |
+ count = 0; |
+ double progress = ((double) i - first) / ((double) part_all); |
+ progress = (double) progress * 100.00; |
+ w_log.println(part + ".-" + ((int) progress) + "%..."); |
+ w_log.flush(); |
+ } |
+ |
+ for (SimpleFeature feature : list_cmzs) { |
+ Geometry geometry = (Geometry) feature.getDefaultGeometry(); |
+ if (AOI.get(i).within(geometry)) { |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
+ } |
+ CMZs.add(no_start_zero); |
+ break; |
+ } else if (AOI.get(i).intersects(geometry) || AOI.get(i).crosses(geometry)) { |
+ Geometry intersection = AOI.get(i).intersection(geometry); |
+ if (intersection instanceof MultiPolygon) { |
+ MultiPolygon mp = (MultiPolygon) intersection; |
+ double sum_area_part = 0; |
+ for (int uu = 0; uu < mp.getNumGeometries(); uu++) { |
+ com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(uu); |
+ Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
+ sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.get(i).getArea()) * 100); |
+ } |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
+ } |
+ if (!CMZs.contains(no_start_zero) && sum_area_part > 10) { |
+ CMZs.add(no_start_zero); |
+ } |
+ } |
+ if (intersection instanceof Polygon) { |
+ com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection; |
+ Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
+ double intersection_area_portion = (gIntersection.getArea() / AOI.get(i).getArea()) * 100; |
+ String no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", ""); |
+ if (feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").startsWith("0")) { |
+ no_start_zero = feature.getProperty("CMZ").getValue().toString().replace("CMZ ", "").replace("0", ""); |
+ } |
+ if (!CMZs.contains(no_start_zero) && intersection_area_portion > 10) { |
+ CMZs.add(no_start_zero); |
+ } |
+ } |
+ } |
+ } |
+ |
+ for (SimpleFeature feature : list_states) { |
+ Geometry geometry = (Geometry) feature.getDefaultGeometry(); |
+ if (AOI.get(i).within(geometry)) { |
+ states.add(feature.getProperty("STATE").getValue().toString().trim()); |
+ break; |
+ } else if (AOI.get(i).intersects(geometry) || AOI.get(i).crosses(geometry)) { |
+ Geometry intersection = AOI.get(i).intersection(geometry); |
+ if (intersection instanceof MultiPolygon) { |
+ MultiPolygon mp = (MultiPolygon) intersection; |
+ double sum_area_part = 0; |
+ for (int uu = 0; uu < mp.getNumGeometries(); uu++) { |
+ com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) mp.getGeometryN(uu); |
+ Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
+ sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.get(i).getArea()) * 100); |
+ } |
+ if (!states.contains(feature.getProperty("STATE").getValue().toString()) && sum_area_part > 10) { |
+ states.add(feature.getProperty("STATE").getValue().toString()); |
+ } |
+ } |
+ if (intersection instanceof Polygon) { |
+ com.vividsolutions.jts.geom.Polygon g = (com.vividsolutions.jts.geom.Polygon) intersection; |
+ Geometry gIntersection = IntersectUtils.intersection(g, AOI.get(i)); |
+ double sum_area_part = 0; |
+ sum_area_part = sum_area_part + ((gIntersection.getArea() / AOI.get(i).getArea()) * 100); |
+ if (!states.contains(feature.getProperty("STATE").getValue().toString()) && sum_area_part > 10) { |
+ states.add(feature.getProperty("STATE").getValue().toString()); |
+ } |
+ } |
+ } |
+ } |
+ |
+ ERU new_hru = new ERU(); |
+ new_hru.ID = i; |
+ new_hru.state = states; |
+ new_hru.cmz = CMZs; |
+ new_hru.multiCMZs = false; |
+ new_hru.multiStates = false; |
+ new_hru.geom = AOI.get(i); |
+ new_hru.irrigated = false; |
+ new_hru.irrigated_area = 0.0; |
+ if (states.size() > 1) { |
+ new_hru.multiStates = true; |
+ } |
+ if (CMZs.size() > 1) { |
+ new_hru.multiCMZs = true; |
+ } |
+ allHRUs.add(new_hru); |
+ } |
+ |
+ return allHRUs; |
} |
|
protected static FeatureCollection featureCollectionFromShapefile(URL shapefile) throws IOException { |
@@ -13,10 +13,7 @@ |
import java.awt.Rectangle; |
import java.awt.geom.AffineTransform; |
import java.io.File; |
-import java.io.FileOutputStream; |
import java.io.IOException; |
-import java.io.InputStream; |
-import java.io.OutputStream; |
import java.io.PrintWriter; |
import java.net.MalformedURLException; |
import java.net.URISyntaxException; |
@@ -38,16 +35,11 @@ |
import java.util.concurrent.ExecutorService; |
import java.util.concurrent.Executors; |
import java.util.concurrent.FutureTask; |
-import javax.xml.parsers.DocumentBuilder; |
-import javax.xml.parsers.DocumentBuilderFactory; |
-import javax.xml.parsers.ParserConfigurationException; |
import lamps.CSIP_Const; |
import methods.objects.ERU; |
import methods.objects.Nass_accurcies; |
import methods.objects.Nass_area_reclassify; |
import methods.objects.Nass_results; |
-import oms3.io.CSTable; |
-import oms3.io.DataIO; |
import org.geotools.coverage.grid.GridCoordinates2D; |
import org.geotools.coverage.grid.GridCoverage2D; |
import org.geotools.coverage.grid.GridGeometry2D; |
@@ -57,19 +49,17 @@ |
import org.geotools.coverage.processing.operation.Crop; |
import org.geotools.data.DataStore; |
import org.geotools.data.FeatureSource; |
-import org.geotools.data.ows.Layer; |
-import org.geotools.data.ows.WMSCapabilities; |
import org.geotools.data.shapefile.ShapefileDataStore; |
-import org.geotools.data.wms.WebMapServer; |
import org.geotools.factory.Hints; |
import org.geotools.feature.FeatureCollection; |
+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.opengis.geometry.Envelope; |
import org.opengis.parameter.ParameterValueGroup; |
+import org.opengis.referencing.FactoryException; |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
-import org.w3c.dom.Document; |
-import org.w3c.dom.Node; |
-import org.w3c.dom.NodeList; |
-import org.xml.sax.SAXException; |
|
/** |
* |
@@ -78,58 +68,15 @@ |
public class CSIP_NASS_GeoTIFF { |
|
/** |
- * Returns the amount of numbers that can be divided by the divisor without |
- * remainder (using parallel execution). |
- * |
- * @param years |
- * @param shp_Extend |
+ * @param CDL_years |
+ * @param Input_Geometries |
+ * @param map |
* @param outputDir |
- * @return Amount of numbers that can be divided by the divisor without |
- * remainder |
+ * @param w_log |
+ * @return |
* @throws InterruptedException |
* @throws ExecutionException |
*/ |
- public static int ConcurrentGeoTIFF_Download(List<String> years, String shp_Extend, File outputDir) |
- throws InterruptedException, ExecutionException { |
- |
- final int threadNum = CSIP_Const.DownloadThreadNumber; |
- final int first = 0; |
- final int last = years.size() - 1; |
- final String shp = shp_Extend; |
- final File od = outputDir; |
- final List<String> yearss = years; |
- |
- final int p = (last + 1) / threadNum; |
- int s = (last + 1) - p * threadNum; |
- int amount = 0; |
- |
- // Prepare to execute and store the Futures |
- ExecutorService executor = Executors.newFixedThreadPool(threadNum); |
- List<FutureTask<Integer>> taskList = new ArrayList<>(); |
- for (int j = 0; j < threadNum; j++) { |
- final int ii = j; |
- FutureTask<Integer> futureTask_1 = new FutureTask<>(new Callable<Integer>() { |
- int inlast = (ii == threadNum - 1 ? last : p + (ii * p)); |
- int infirst = (ii == 0 ? first : (p * ii) + 1); |
- |
- @Override |
- public Integer call() throws Exception { |
- return CSIP_NASS_GeoTIFF.Download_NASSGeoTIFFs(infirst, inlast, yearss, shp, od); |
- } |
- }); |
- taskList.add(futureTask_1); |
- executor.execute(futureTask_1); |
- } |
- |
- // Wait until all results are available and combine them at the same time |
- for (int j = 0; j < threadNum; j++) { |
- FutureTask<Integer> futureTask = taskList.get(j); |
- amount += futureTask.get(); |
- } |
- executor.shutdown(); |
- return amount; |
- } |
- |
public static ArrayList<Nass_results> ConcurrentGeoTIFF_Clipping(List<String> CDL_years, List<Geometry> Input_Geometries, HashMap<Integer, String> map, File outputDir, PrintWriter w_log) throws InterruptedException, ExecutionException { |
|
int threadnr = 1; |
@@ -179,6 +126,15 @@ |
return result_list; |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param Input_Geometries |
+ * @param w_log |
+ * @return |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
public static HashMap<Integer, ArrayList<HashMap<Integer, Double>>> ConcurrentIrrigation_Overlay(File dataDir, List<Geometry> Input_Geometries, PrintWriter w_log) |
throws InterruptedException, ExecutionException { |
|
@@ -203,6 +159,7 @@ |
|
for (int i = 0; i < years.length; i++) { |
final int pos = i; |
+ long start_time = System.currentTimeMillis(); |
w_log.println(" year: " + years[pos]); |
w_log.flush(); |
// Prepare to execute and store the Futures |
@@ -245,18 +202,30 @@ |
} |
} |
executor.shutdown(); |
+ long end_time = System.currentTimeMillis(); |
+ double difference = (end_time - start_time) / 1000; |
+ w_log.println(" Irrigation raster duration: " + difference + " s"); |
+ w_log.flush(); |
} |
return result_map; |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param st |
+ * @param w_log |
+ * @return |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
public static List<String> NASS_CDL_YEARS_CHECK(File dataDir, List<String> st, PrintWriter w_log) throws ClassNotFoundException, SQLException { |
Class.forName("org.h2.Driver"); |
List<String> years; |
- try (Connection conn = DriverManager.getConnection("jdbc:h2:file:" + dataDir.getPath() + CSIP_Const.LocalDB, "sa", "")) { |
- Statement state = conn.createStatement(); |
+ try (Connection conn = DriverManager.getConnection("jdbc:h2:file:" + dataDir.getPath() + CSIP_Const.LocalDB, "sa", ""); |
+ Statement state = conn.createStatement()) { |
years = new ArrayList<>(); |
for (int x = 0; x < st.size(); x++) { |
- |
String queryString = "select * from \"PUBLIC\".CDL_YEARS where acr='" + st.get(x) + "'"; |
ResultSet result = state.executeQuery(queryString); |
while (result.next()) { |
@@ -266,114 +235,17 @@ |
} |
} |
} |
- |
- } |
- state.close(); |
- } |
- return years; |
- } |
- |
- public static List<String> wms(List<String> st, PrintWriter w_log) throws Exception { |
- w_log.println(" "); |
- w_log.println(" Checking NASS CDL Server for available years..."); |
- URL url = null; |
- List<String> years = new ArrayList<>(); |
- |
- for (int x = 0; x < st.size(); x++) { |
- |
- url = new URL("http://129.174.131.7/cgi/wms_cdl_" + st.get(x) + ".cgi?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities"); |
- WebMapServer wms = new WebMapServer(url); |
- WMSCapabilities capabilities = wms.getCapabilities(); |
- List<Layer> all = capabilities.getLayerList(); |
- |
- for (int i = all.size() - 1; i > 0; i--) { |
- String[] help0 = all.get(i).getName().split("cdl_"); |
- String[] help1 = help0[1].split("_"); |
- if (!years.contains(help1[0])) { |
- years.add(help1[0]); |
- } |
- |
} |
} |
return years; |
} |
|
- public static HashMap<String, List<Integer>> wms_hash(List<String> st, PrintWriter w_log) throws Exception { |
- w_log.println(" "); |
- w_log.println(" Checking NASS CDL Server for available years..."); |
- HashMap<String, List<Integer>> map = new HashMap<>(); |
- URL url = null; |
- |
- for (int x = 0; x < st.size(); x++) { |
- List<Integer> years = new ArrayList<>(); |
- url = new URL("http://129.174.131.7/cgi/wms_cdl_" + st.get(x) + ".cgi?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetCapabilities"); |
- //System.out.println("Beginning HTTP GeoTIFF request..." + url); |
- WebMapServer wms = new WebMapServer(url); |
- WMSCapabilities capabilities = wms.getCapabilities(); |
- List<Layer> all = capabilities.getLayerList(); |
- |
- for (int i = all.size() - 1; i > 0; i--) { |
- //System.out.println(" "+all.get(i).getName()); |
- String[] help0 = all.get(i).getName().split("cdl_"); |
- String[] help1 = help0[1].split("_"); |
- //System.out.print(" " + help1[0]); |
- |
- if (!years.contains(Integer.parseInt(help1[0]))) { |
- years.add(Integer.parseInt(help1[0])); |
- } |
- |
- } |
- |
- Collections.sort(years); |
- |
- map.put(st.get(x), years); |
- } |
- |
- w_log.println(" "); |
- return map; |
- } |
- |
- public static int Download_NASSGeoTIFFs(int first, int last, List<String> years, String shp_Extend, File outputDir) throws MalformedURLException, IOException, ParserConfigurationException, SAXException { |
- String CropScapeJsonURL = ""; |
- int message = 0; |
- for (int i = first; i <= last; i++) { |
- //System.out.println(" "); |
- String baseURL = "http://nassgeodata.gmu.edu:8080/axis2/services/CDLService/GetCDLFile?year=" + years.get(i) + shp_Extend; |
- //System.out.println("Beginning HTTP GeoTIFF request..." + baseURL); |
- |
- URL url = new URL(baseURL); |
- |
- InputStream is = url.openStream(); |
- DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
- DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
- Document doc = dBuilder.parse(is); |
- doc.getDocumentElement().normalize(); |
- NodeList nList = doc.getElementsByTagName("returnURL"); |
- |
- for (int temp = 0; temp < nList.getLength(); temp++) { |
- Node nNode = nList.item(temp); |
- CropScapeJsonURL = nNode.getTextContent() + ""; |
- } |
- |
- URL url1 = new URL(CropScapeJsonURL); |
- File f; |
- try (InputStream geotiffInputStream = url1.openStream()) { |
- f = new File(outputDir, "NASS_" + years.get(i) + ".tif"); |
- try (OutputStream out = new FileOutputStream(f)) { |
- int read = 0; |
- byte[] bytes = new byte[4096]; |
- while ((read = geotiffInputStream.read(bytes)) != -1) { |
- out.write(bytes, 0, read); |
- } |
- out.flush(); |
- } |
- } |
- System.out.println("Wrote : " + f); |
- message++; |
- } |
- return message; |
- } |
- |
+ /** |
+ * |
+ * @param shapefile |
+ * @return |
+ * @throws IOException |
+ */ |
protected static FeatureCollection featureCollectionFromShapefile(URL shapefile) throws IOException { |
DataStore dataStore = new ShapefileDataStore(shapefile); |
FeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]); |
@@ -400,6 +272,14 @@ |
} |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param w_log |
+ * @return |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
public static HashMap<Integer, String> DB_Query_NASS_Raster_IDs(File dataDir, PrintWriter w_log) throws ClassNotFoundException, SQLException { |
|
HashMap<Integer, String> map = new HashMap<>(); |
@@ -413,12 +293,21 @@ |
} |
state.close(); |
} |
- w_log.println(" Number of NASS provided vegetation names: " + map.size()); |
- w_log.println(" "); |
return map; |
} |
|
- public static HashMap<String, ArrayList<Nass_accurcies>> Read_nassaccu_csv(File dataDir, ArrayList<String> allcrops, List<String> states, PrintWriter w_log) throws IOException { |
+ /** |
+ * |
+ * @param dataDir |
+ * @param allcrops |
+ * @param states |
+ * @param w_log |
+ * @return |
+ * @throws IOException |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
+ public static HashMap<String, ArrayList<Nass_accurcies>> DB_nassaccu(File dataDir, ArrayList<String> allcrops, List<String> states, PrintWriter w_log) throws IOException, ClassNotFoundException, SQLException { |
|
HashMap<String, ArrayList<Nass_accurcies>> prep_map = new HashMap<>(); |
|
@@ -442,50 +331,41 @@ |
|
HashMap<String, ArrayList<Nass_accurcies>> map = new HashMap<>(); |
|
- File file = new File(dataDir, CSIP_Const.ACC); |
+ Class.forName("org.h2.Driver"); |
+ try (Connection conn = DriverManager.getConnection("jdbc:h2:file:" + dataDir.getPath() + CSIP_Const.LocalDB, "sa", "")) { |
+ try (Statement state = conn.createStatement()) { |
|
- if (DataIO.tableExists("Parameter", file)) { |
- CSTable table = DataIO.table(file.toURI().toURL(), "Parameter"); |
- int len = table.getColumnCount() - 2; |
- String oldname = "-"; |
- String name = "-"; |
- String in_name = "-"; |
- ArrayList<Nass_accurcies> list = null; |
- int start_year = 2008; |
- for (String[] row : table.rows()) { |
- for (int i = 0; i < states.size(); i++) { |
- String state = states.get(i); |
- if (!row[2].isEmpty() && row[2].equals(state)) { |
- name = row[1]; |
- if (allcrops.contains(name)) { |
- in_name = name; |
- double nass_accuracy[] = new double[len]; |
- int nass_accu_years[] = new int[len]; |
- for (int a = 0; a < len; a++) { |
- if (!row[a + 3].isEmpty()) { |
- nass_accuracy[a] = Double.parseDouble(row[a + 3]); |
+ for (String crop : allcrops) { |
+ ArrayList<Nass_accurcies> list = new ArrayList<>(); |
+ for (String stateoutlist : states) { |
+ double nass_accuracy[] = new double[CSIP_Const.ACC_years.length]; |
+ int nass_accu_years[] = new int[CSIP_Const.ACC_years.length]; |
+ int rr = 0; |
+ for (String accyear : CSIP_Const.ACC_years) { |
+ String queryString = "select " + stateoutlist + " from \"PUBLIC\".ACCURACY" + accyear + " where cropname='" + crop + "'"; |
+// w_log.println(" " + queryString); |
+// w_log.flush(); |
+ ResultSet result = state.executeQuery(queryString); |
+ while (result.next()) { |
+ if (!result.getString(stateoutlist).equals("-")) { |
+ nass_accuracy[rr] = Double.parseDouble(result.getString(stateoutlist)); |
} else { |
- nass_accuracy[a] = 0.0; |
+ nass_accuracy[rr] = 0.0; |
} |
- nass_accu_years[a] = start_year + a; |
+ |
+ nass_accu_years[rr] = Integer.parseInt(accyear); |
} |
- if (!in_name.equals(oldname)) {//&& !row[2].isEmpty() |
- if (map.put(oldname, list) != null) { |
- throw new IllegalArgumentException("Duplicate name: " + oldname); |
- } |
- list = new ArrayList<>(); |
- oldname = in_name; |
- } |
- list.add(new Nass_accurcies(state, nass_accuracy, nass_accu_years)); |
+ rr++; |
} |
+ list.add(new Nass_accurcies(stateoutlist, nass_accuracy, nass_accu_years)); |
+ } |
+ if (map.put(crop, list) != null) { |
+ throw new IllegalArgumentException("Duplicate name: " + crop); |
} |
} |
+ state.close(); |
} |
- if (map.put(in_name, list) != null) { |
- throw new IllegalArgumentException("Duplicate name: " + oldname); |
- } |
- |
- w_log.println("[" + ((int) map.size() - 1) + " NASS crop names from nass_accuracy.csv ]"); |
+ conn.close(); |
} |
|
//TODO have to be much more HashMappy ;-) |
@@ -511,6 +391,20 @@ |
return prep_map; |
} |
|
+ /** |
+ * |
+ * @param first |
+ * @param last |
+ * @param pos |
+ * @param dataDir |
+ * @param Input_Geometries |
+ * @param ye |
+ * @param w_log |
+ * @return |
+ * @throws MalformedURLException |
+ * @throws IllegalArgumentException |
+ * @throws IOException |
+ */ |
public static HashMap<Integer, Double> IRRIGATION_clip_analysis(int first, int last, int pos, File dataDir, List<Geometry> Input_Geometries, String ye, PrintWriter w_log) throws MalformedURLException, IllegalArgumentException, IOException { |
HashMap<Integer, Double> map = new HashMap<>(); |
|
@@ -534,65 +428,94 @@ |
CoordinateReferenceSystem crs = reader.getCrs(); |
hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs); |
|
+ boolean on = false; |
+ |
for (int j = first; j <= last; j++) { |
|
- LinearRing lr = null; |
+ if (!on) { |
+ LinearRing lr = null; |
|
- Geometry targetGeometry = Input_Geometries.get(j); |
+ Geometry targetGeometry = Input_Geometries.get(j); |
|
- if (!targetGeometry.isValid()) { |
- targetGeometry = targetGeometry.convexHull(); |
- } |
+ if (!targetGeometry.isValid()) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ } |
|
- int last_coo = targetGeometry.getCoordinates().length - 1; |
- if (!targetGeometry.getCoordinates()[0].equals(targetGeometry.getCoordinates()[last_coo])) { |
- targetGeometry = targetGeometry.convexHull(); |
- } |
+ int last_coo = targetGeometry.getCoordinates().length - 1; |
+ if (!targetGeometry.getCoordinates()[0].equals(targetGeometry.getCoordinates()[last_coo])) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ } |
|
- lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
+ lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
|
- if (!lr.isValid()) { |
- targetGeometry = targetGeometry.convexHull(); |
- lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
- } |
+ if (!lr.isValid()) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
+ } |
|
- Polygon crop_poly = new Polygon(lr, null, geometryFactory); |
+ Polygon crop_poly = new Polygon(lr, null, geometryFactory); |
+ Polygon crop_poly_org = crop_poly; |
|
- double min_area = 5000; |
+ Polygon simpleP = (Polygon) TopologyPreservingSimplifier.simplify(crop_poly, 1); |
|
- if (crop_poly.getArea() <= min_area) { |
- int sizer = 5; |
- while (crop_poly.getArea() < min_area) { |
- crop_poly = (Polygon) crop_poly.buffer(sizer); |
- sizer = sizer + 1; |
- } |
- } |
+ CropParameterValueGroup.parameter("SOURCE").setValue(gcsaga); |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
|
- Polygon simpleP = (Polygon) TopologyPreservingSimplifier.simplify(crop_poly, 1); |
+ boolean success = false; |
+ double sizer = 0.01; |
|
- CropParameterValueGroup.parameter("SOURCE").setValue(gcsaga); |
- CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
- GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
- int values = 0; |
+ try { |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ success = true; |
+ throw new Exception(); |
+ } catch (Exception e) { |
+ } finally { |
+ if (!success) { |
+ //System.out.println(j + " No Irrigation overlay possible !!!!!!!!!!!!!!!!"); |
+ while (!success) { |
+ crop_poly = (Polygon) crop_poly.buffer(sizer); |
+ sizer = sizer + 0.01; |
+ simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
+ try { |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ success = true; |
+ throw new Exception(); |
+ } catch (Exception e) { |
+ } finally { |
|
- GridGeometry2D gg = gcCrop.getGridGeometry(); |
- double data[] = new double[1]; |
- Rectangle gridBoundss = gg.getGridRange2D().getBounds(); |
- for (int y1 = gridBoundss.y, ny = 0; ny < gridBoundss.height; y1++, ny++) { |
- for (int x = gridBoundss.x, nx = 0; nx < gridBoundss.width; x++, nx++) { |
- GridCoordinates2D gridCoord = new GridCoordinates2D(x, y1); |
- gcCrop.evaluate(gridCoord, data); |
- if (data[0] == 1) { |
- values++; |
+ } |
+ } |
+ crop_poly = (Polygon) crop_poly.buffer(sizer); |
+ simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
+ //System.out.println(j + " fine_org: " + crop_poly_org.getArea() + " new: " + crop_poly.getArea() + " sizer: " + sizer + " success " + success); |
} |
} |
- } |
- if (values > 0) { |
- double irri_size = values * (cs * cs); |
- double frac_irri = (irri_size / simpleP.getArea()) * 100; |
- if (frac_irri > 40) { |
- w_log.println(j + " Polygon Area: " + simpleP.getArea() + " irri Area: " + irri_size + " irrigated " + frac_irri + "%"); |
- map.put(j, frac_irri); |
+ |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ int values = 0; |
+ |
+ GridGeometry2D gg = gcCrop.getGridGeometry(); |
+ double data[] = new double[1]; |
+ Rectangle gridBoundss = gg.getGridRange2D().getBounds(); |
+ for (int y1 = gridBoundss.y, ny = 0; ny < gridBoundss.height; y1++, ny++) { |
+ for (int x = gridBoundss.x, nx = 0; nx < gridBoundss.width; x++, nx++) { |
+ GridCoordinates2D gridCoord = new GridCoordinates2D(x, y1); |
+ gcCrop.evaluate(gridCoord, data); |
+ if (data[0] == 1) { |
+ values++; |
+ } |
+ } |
+ } |
+ |
+ if (values > 0) { |
+ double irri_size = values * (cs * cs); |
+ double frac_irri = (irri_size / simpleP.getArea()) * 100; |
+ if (frac_irri > 0) { |
+ w_log.println(j + " Polygon Area: " + simpleP.getArea() + " irri Area: " + irri_size + " irrigated " + frac_irri + "%"); |
+ map.put(j, frac_irri); |
+ } |
} |
} |
} |
@@ -601,6 +524,24 @@ |
return map; |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param years |
+ * @param shp_Extend |
+ * @param outputDir |
+ * @param Input_Geometries |
+ * @param hrus |
+ * @param unique_states |
+ * @param w_log |
+ * @return |
+ * @throws IOException |
+ * @throws URISyntaxException |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
public static HashMap<Integer, ArrayList<Nass_results>> NASSGeoTIFF_clip_analysis(File dataDir, List<String> years, String shp_Extend, File outputDir, List<Geometry> Input_Geometries, List<ERU> hrus, List<String> unique_states, PrintWriter w_log) throws IOException, URISyntaxException, ClassNotFoundException, SQLException, InterruptedException, ExecutionException { |
|
w_log.println(" "); |
@@ -614,6 +555,22 @@ |
ArrayList<Nass_results> cropslist = new ArrayList<>(); |
|
cropslist = CSIP_NASS_GeoTIFF.ConcurrentGeoTIFF_Clipping(years, Input_Geometries, NASS_map, outputDir, w_log); |
+ //cropslist.add(new Nass_results(years.get(i), y, simpleP.getArea(), clip_size, crops_names, crops_area, is_crop, crops_area)); |
+ //y++; |
+ |
+ List<Nass_results> NASS_list = cropslist; |
+ |
+ for (ERU hru : hrus) { |
+ List<String> NASS_print_list = new ArrayList<>(); |
+ for (int v = 0; v < NASS_list.size(); v++) { |
+ if (hru.ID == NASS_list.get(v).getID()) { |
+ for (int k = 0; k < NASS_list.get(v).getcrop_nass_names().length; k++) { |
+ NASS_print_list.add("," + NASS_list.get(v).getID() + "," + NASS_list.get(v).getyear() + "," + NASS_list.get(v).getclip_grid_area() + "," + NASS_list.get(v).getpolygon_area() + "," + NASS_list.get(v).getcrop_nass_names()[k] + "," + NASS_list.get(v).getcrop_area()[k]); |
+ } |
+ } |
+ } |
+ hru.nass_plain_list = NASS_print_list; |
+ } |
|
ArrayList<String> all_crops = new ArrayList<>(); |
for (Nass_results NR : cropslist) { |
@@ -632,7 +589,7 @@ |
|
HashMap<String, ArrayList<Nass_accurcies>> map = new HashMap<>(); |
|
- map = CSIP_NASS_GeoTIFF.Read_nassaccu_csv(dataDir, all_crops, unique_states, w_log); |
+ map = CSIP_NASS_GeoTIFF.DB_nassaccu(dataDir, all_crops, unique_states, w_log); |
|
HashMap<Integer, ArrayList<Nass_results>> ALL_HRU_CROPS = new HashMap(); |
|
@@ -643,108 +600,149 @@ |
return ALL_HRU_CROPS; |
} |
|
- public static ArrayList<Nass_results> NASS_GeoTIFF_clipping(int first, int last, HashMap<Integer, String> map, List<String> years, List<Geometry> Input_Geometries, File outputDir, PrintWriter w_log) throws IllegalArgumentException, IOException { |
+ public static ArrayList<Nass_results> NASS_GeoTIFF_clipping(int first, int last, HashMap<Integer, String> map, List<String> years, List<Geometry> Input_Geometries, File outputDir, PrintWriter w_log) throws IllegalArgumentException, IOException, ExecutionException, FactoryException { |
|
ArrayList<Nass_results> cropslist = new ArrayList<>(); |
|
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); |
+ CoordinateReferenceSystem targetCRS = CRS.parseWKT(CSIP_Const.WKT_CooSyst); |
+ |
+ double min_x = 28000000; |
+ double max_x = -28000000; |
+ double min_y = 3000000; |
+ double max_y = 280000; |
+ |
for (int i = first; i <= last; i++) { |
File NASS_file = new File(outputDir, "NASS_" + years.get(i) + ".tif"); |
if (NASS_file.exists()) { |
+ w_log.println(" Processing year: " + years.get(i)); |
+ w_log.flush(); |
AbstractGridCoverage2DReader reader; |
AbstractGridFormat format = GridFormatFinder.findFormat(NASS_file); |
reader = format.getReader(NASS_file); |
|
final GridCoverage2D gc = (GridCoverage2D) reader.read(null); |
- AffineTransform gridToCRS_gc = (AffineTransform) gc.getGridGeometry().getGridToCRS2D(); |
- double cell_size_bgrid = org.geotools.referencing.operation.matrix.XAffineTransform.getScaleX0(gridToCRS_gc); |
+ min_x = gc.getEnvelope().getLowerCorner().getCoordinate()[0]; |
+ min_y = gc.getEnvelope().getLowerCorner().getCoordinate()[1]; |
+ max_x = gc.getEnvelope().getUpperCorner().getCoordinate()[0]; |
+ max_y = gc.getEnvelope().getUpperCorner().getCoordinate()[1]; |
+ |
+ Geometry NASS_raster_envelope = JTS.toGeometry(new ReferencedEnvelope(min_x, max_x, min_y, max_y, targetCRS)); |
|
Crop ImageCrop = new Crop(); |
|
ParameterValueGroup CropParameterValueGroup = ImageCrop.getParameters(); |
CropParameterValueGroup.parameter("Source").setValue(gc); |
|
+ AffineTransform gridToCRS = (AffineTransform) gc.getGridGeometry().getGridToCRS2D(); |
+ double cell_size = org.geotools.referencing.operation.matrix.XAffineTransform.getScaleX0(gridToCRS); |
+ |
final Hints hints = new Hints(); |
CoordinateReferenceSystem crs = reader.getCrs(); |
hints.put(Hints.DEFAULT_COORDINATE_REFERENCE_SYSTEM, crs); |
|
int y = 0; |
for (int bj = 0; bj < Input_Geometries.size(); bj++) { |
-// w_log.println(" ID : " + bj + " year: " + years.get(i)); |
-// w_log.flush(); |
- |
- LinearRing lr = null; |
- |
- Geometry targetGeometry = Input_Geometries.get(bj); |
- |
- if (!targetGeometry.isValid()) { |
- targetGeometry = targetGeometry.convexHull(); |
- } |
- |
- int last_point = targetGeometry.getCoordinates().length - 1; |
- if (!targetGeometry.getCoordinates()[0].equals(targetGeometry.getCoordinates()[last_point])) { |
- targetGeometry = targetGeometry.convexHull(); |
- } |
- |
- lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
- |
- if (!lr.isValid()) { |
- targetGeometry = targetGeometry.convexHull(); |
- lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
- } |
- |
- Polygon crop_poly = new Polygon(lr, null, geometryFactory); |
- |
- double min_area = 0; |
- if (cell_size_bgrid > 30) { |
- min_area = 14000; |
- } else { |
- min_area = 2000; |
- } |
- |
- if (crop_poly.getArea() <= min_area) { |
- int sizer = 5; |
- while (crop_poly.getArea() < min_area) { |
- crop_poly = (Polygon) crop_poly.buffer(sizer); |
- sizer = sizer + 1; |
- } |
- } |
- |
- //Polygon simpleP = (Polygon) TopologyPreservingSimplifier.simplify(crop_poly, 1); |
- Polygon simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
- |
- CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
- //CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(crop_poly); |
- |
- GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
- |
- AffineTransform gridToCRS = (AffineTransform) gc.getGridGeometry().getGridToCRS2D(); |
- double cell_size = org.geotools.referencing.operation.matrix.XAffineTransform.getScaleX0(gridToCRS); |
|
HashMap<Integer, Integer> Pixel_NASSID_map = new HashMap<>(); |
int values = 0; |
|
- GridGeometry2D gg = gcCrop.getGridGeometry(); |
- int data[] = new int[1]; |
- Rectangle gridBoundss = gg.getGridRange2D().getBounds(); |
- for (int y1 = gridBoundss.y, ny = 0; ny < gridBoundss.height; y1++, ny++) { |
- for (int x = gridBoundss.x, nx = 0; nx < gridBoundss.width; x++, nx++) { |
- GridCoordinates2D gridCoord = new GridCoordinates2D(x, y1); |
- gcCrop.evaluate(gridCoord, data); |
- if (data[0] > 0) { |
- if (Pixel_NASSID_map.get(data[0]) == null) { |
- Pixel_NASSID_map.put(data[0], 1); |
- } else { |
- Pixel_NASSID_map.put(data[0], (Pixel_NASSID_map.get(data[0])) + 1); |
+ Geometry targetGeometry = Input_Geometries.get(bj); |
+ LinearRing lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
+ Polygon crop_poly = new Polygon(lr, null, geometryFactory); |
+ Polygon crop_poly_org = crop_poly; |
+ |
+ if (Input_Geometries.get(bj).intersects(NASS_raster_envelope)) { |
+ |
+ if (!targetGeometry.isValid()) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ } |
+ |
+ int last_point = targetGeometry.getCoordinates().length - 1; |
+ if (!targetGeometry.getCoordinates()[0].equals(targetGeometry.getCoordinates()[last_point])) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ } |
+ |
+ lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
+ |
+ if (!lr.isValid()) { |
+ targetGeometry = targetGeometry.convexHull(); |
+ lr = geometryFactory.createLinearRing(targetGeometry.getCoordinates()); |
+ } |
+ |
+ crop_poly = new Polygon(lr, null, geometryFactory); |
+ crop_poly_org = crop_poly; |
+ |
+ //Polygon simpleP = (Polygon) TopologyPreservingSimplifier.simplify(crop_poly, 1); |
+ Polygon simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
+ |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
+ //CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(crop_poly); |
+ |
+ boolean success = false; |
+ double sizer = 0.01; |
+ |
+ try { |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ success = true; |
+ throw new Exception(); |
+ |
+ } catch (Exception e) { |
+ |
+ } finally { |
+ if (!success) { |
+ //System.out.println(bj + " No Cropping possible !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " + years.get(i)); |
+ while (!success) { |
+ crop_poly = (Polygon) crop_poly.buffer(sizer); |
+ sizer = sizer + 0.01; |
+ simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
+ try { |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ success = true; |
+ throw new Exception(); |
+ } catch (Exception e) { |
+ } finally { |
+ |
+ } |
} |
- values++; |
+ crop_poly = (Polygon) crop_poly.buffer(sizer); |
+ simpleP = (Polygon) DouglasPeuckerSimplifier.simplify(crop_poly, 0.1); |
+ CropParameterValueGroup.parameter(Crop.PARAMNAME_ROI).setValue(simpleP); |
+ //System.out.println(bj + " fine_org: " + crop_poly_org.getArea() + " new: " + crop_poly.getArea() + " sizer: " + sizer + " success " + success); |
+ } |
+ } |
+ |
+ GridCoverage2D gcCrop = (GridCoverage2D) ImageCrop.doOperation(CropParameterValueGroup, hints); |
+ |
+ ArrayList pixvalues = new ArrayList<>(); |
+ |
+ GridGeometry2D gg = gcCrop.getGridGeometry(); |
+ int data[] = new int[1]; |
+ Rectangle gridBoundss = gg.getGridRange2D().getBounds(); |
+ for (int y1 = gridBoundss.y, ny = 0; ny < gridBoundss.height; y1++, ny++) { |
+ for (int x = gridBoundss.x, nx = 0; nx < gridBoundss.width; x++, nx++) { |
+ GridCoordinates2D gridCoord = new GridCoordinates2D(x, y1); |
+ gcCrop.evaluate(gridCoord, data); |
+ if (data[0] > 0) { |
+ if (Pixel_NASSID_map.get(data[0]) == null) { |
+ Pixel_NASSID_map.put(data[0], 1); |
+ } else { |
+ Pixel_NASSID_map.put(data[0], (Pixel_NASSID_map.get(data[0])) + 1); |
+ } |
+ values++; |
+ } |
+ if (!pixvalues.contains(data[0])) { |
+ pixvalues.add(data[0]); |
+ } |
} |
} |
} |
if (values == 0) { |
- //w_log.println(" ERROR No Crop-values in CropGrid!!!!!!!!!!!!!!!!!!!!!!!!!! NASS_" + years.get(i) + ".tif"); |
values = 1; |
- Pixel_NASSID_map.put(81, 1); |
+ Pixel_NASSID_map.put(255, 1); |
+// w_log.println(" No Crop-values in CropGrid!!!!!!!!!!!!!!!!!!!!!!!!!! NASS_" + years.get(i) + ".tif"); |
+// w_log.flush(); |
} |
|
double clip_size = (cell_size * cell_size) * values; |
@@ -758,14 +756,26 @@ |
crops_area[cc] = (double) entry.getValue() * (cell_size * cell_size); |
cc++; |
} |
- cropslist.add(new Nass_results(years.get(i), y, simpleP.getArea(), clip_size, crops_names, crops_area, is_crop, crops_area)); |
+ cropslist.add(new Nass_results(years.get(i), y, crop_poly_org.getArea(), clip_size, crops_names, crops_area, is_crop, crops_area)); |
y++; |
+ |
} |
} |
} |
return cropslist; |
} |
|
+ /** |
+ * |
+ * @param map |
+ * @param cropslist |
+ * @param accu_year_list |
+ * @param hrus |
+ * @param w_log |
+ * @return |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
public static HashMap<Integer, ArrayList<Nass_results>> ConcurrentHRU_NASS(HashMap<String, ArrayList<Nass_accurcies>> map, ArrayList<Nass_results> cropslist, ArrayList accu_year_list, List<ERU> hrus, PrintWriter w_log) |
throws InterruptedException, ExecutionException { |
|
@@ -820,21 +830,31 @@ |
return All_HRUs_Linked; |
} |
|
+ /** |
+ * |
+ * @param first |
+ * @param last |
+ * @param map |
+ * @param cropslist |
+ * @param accu_year_list |
+ * @param hrus |
+ * @param w_log |
+ * @return |
+ * @throws IllegalArgumentException |
+ * @throws IOException |
+ */ |
public static HashMap<Integer, ArrayList<Nass_results>> HRU_NASS_GeoTIFF(int first, int last, HashMap<String, ArrayList<Nass_accurcies>> map, ArrayList<Nass_results> cropslist, ArrayList accu_year_list, List<ERU> hrus, PrintWriter w_log) throws IllegalArgumentException, IOException { |
|
HashMap<Integer, ArrayList<Nass_results>> resultmap = new HashMap<>(); |
|
-// w_log.println(" first: " + first + " ,last: " + last + " cropslist size: " + cropslist.size()); |
-// w_log.flush(); |
-// w_log.println(" "); |
- // w_log.flush(); |
for (int xx = first; xx <= last; xx++) { |
ArrayList<Nass_results> cropslist2 = new ArrayList<>(); |
for (int i = 0; i < cropslist.size(); i++) { //for all crops per year |
- // w_log.println("YEAR: " + cropslist.get(i).getyear()); |
- // w_log.flush(); |
ArrayList<Nass_area_reclassify> list = new ArrayList<>(); |
if (xx == cropslist.get(i).getID()) { // link HRU ID to geometry ID |
+// w_log.println(" ID: " + xx); |
+// w_log.println(" YEAR: " + cropslist.get(i).getyear()); |
+// w_log.flush(); |
//System.out.print(" " + cropslist.get(i).getclip_grid_area() + " m2"); |
//reclassify with NASS accu values and save 1st and 2nd dominat crops with area size |
if (accu_year_list.contains(cropslist.get(i).getyear())) { |
@@ -846,9 +866,11 @@ |
for (int l = 0; l < nass_accu_list.size(); l++) { |
String state = nass_accu_list.get(l).getstate(); |
for (int m = 0; m < nass_accu_list.get(l).getaccus().length; m++) { |
+// w_log.println(" StateSize: " + hrus.get(xx).state.size()+" "+state+" NassAccuListSize: "+nass_accu_list.size()); |
+// w_log.flush(); |
if (hrus.get(xx).state.get(0).equals(state) && Integer.parseInt(cropslist.get(i).getyear()) == nass_accu_list.get(l).getyears()[m]) { |
if (nass_accu_list.get(l).getaccus()[m] == 0.0) { |
- list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], 0.5, 0.999)); |
+ list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], 0.7, 0.999)); |
} else { |
list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], nass_accu_list.get(l).getaccus()[m], 0.999)); |
|
@@ -943,10 +965,10 @@ |
} else { // NO NASS accu available take 1st and 2nd dominat crop without reclassify |
for (int k = 0; k < cropslist.get(i).getcrop_nass_names().length; k++) { |
|
- double check_conf_new = (cropslist.get(i).getcrop_area()[k] / cropslist.get(i).getclip_grid_area()) * 0.5; |
+ double check_conf_new = (cropslist.get(i).getcrop_area()[k] / cropslist.get(i).getclip_grid_area()) * 1; |
|
//list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], 0.5, cropslist.get(i).getcrop_area()[k])); |
- list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], 0.5, check_conf_new)); |
+ list.add(new Nass_area_reclassify(cropslist.get(i).getcrop_nass_names()[k], cropslist.get(i).getcrop_area()[k], cropslist.get(i).getcrop_area()[k], 1, check_conf_new)); |
|
} |
|
@@ -9,7 +9,6 @@ |
import java.io.FileWriter; |
import java.io.IOException; |
import java.io.PrintWriter; |
-import java.net.URISyntaxException; |
import java.sql.Connection; |
import java.sql.DriverManager; |
import java.sql.ResultSet; |
@@ -40,6 +39,16 @@ |
*/ |
public class CSIP_NASS_LMOD_Matching { |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param hrus |
+ * @param unique_cmzs |
+ * @param LMOD_CMZ_Data |
+ * @param DB_LMOD_map |
+ * @param w_log |
+ * @throws Exception |
+ */ |
public static void query_LAMPS_DB_Rotations_For_CMZ(File dataDir, List<ERU> hrus, List<String> unique_cmzs, |
List<ArrayList<String>> LMOD_CMZ_Data, HashMap<String, List<ArrayList<String>>> DB_LMOD_map, PrintWriter w_log) throws Exception { |
|
@@ -69,7 +78,7 @@ |
ArrayList<String> LMOD_data_by_CMZ = new ArrayList<>(); |
List<ArrayList<String>> LMOD_data_by_CMZ_list = new ArrayList<>(); |
LMOD_data_by_CMZ.add(result.getString("KEY")); |
- LMOD_data_by_CMZ.add("" + Integer.parseInt(unique_cmzs.get(i)) + ""); |
+ LMOD_data_by_CMZ.add("" + no_start_zero + ""); |
LMOD_data_by_CMZ.add(result.getString("IRRIGATED")); |
LMOD_data_by_CMZ.add(result.getString("COVERCROPS")); |
LMOD_data_by_CMZ.add(result.getString("CROPSEQUENCE")); |
@@ -77,12 +86,12 @@ |
LMOD_data_by_CMZ.add(result.getString("YEARS")); |
LMOD_CMZ_Data.add(LMOD_data_by_CMZ); |
LMOD_data_by_CMZ_list.add(LMOD_data_by_CMZ); |
- if (DB_LMOD_map.get(result.getString("CROPSEQACR") + "_" + "" + Integer.parseInt(unique_cmzs.get(i)) + "" + "_" + result.getString("IRRIGATED")) == null) { |
- DB_LMOD_map.put(result.getString("CROPSEQACR") + "_" + "" + Integer.parseInt(unique_cmzs.get(i)) + "" + "_" + result.getString("IRRIGATED"), LMOD_data_by_CMZ_list); |
+ if (DB_LMOD_map.get(result.getString("CROPSEQACR") + "_" + "" + no_start_zero + "" + "_" + result.getString("IRRIGATED")) == null) { |
+ DB_LMOD_map.put(result.getString("CROPSEQACR") + "_" + "" + no_start_zero + "" + "_" + result.getString("IRRIGATED"), LMOD_data_by_CMZ_list); |
} else { |
- LMOD_data_by_CMZ_list = DB_LMOD_map.get(result.getString("CROPSEQACR") + "_" + "" + Integer.parseInt(unique_cmzs.get(i)) + "" + "_" + result.getString("IRRIGATED")); |
+ LMOD_data_by_CMZ_list = DB_LMOD_map.get(result.getString("CROPSEQACR") + "_" + "" + no_start_zero + "" + "_" + result.getString("IRRIGATED")); |
LMOD_data_by_CMZ_list.add(LMOD_data_by_CMZ); |
- DB_LMOD_map.put(result.getString("CROPSEQACR") + "_" + "" + Integer.parseInt(unique_cmzs.get(i)) + "" + "_" + result.getString("IRRIGATED"), LMOD_data_by_CMZ_list); |
+ DB_LMOD_map.put(result.getString("CROPSEQACR") + "_" + "" + no_start_zero + "" + "_" + result.getString("IRRIGATED"), LMOD_data_by_CMZ_list); |
// w_log.println(" double_seq_acr : " + result.getString("CROPSEQACR")); |
// w_log.flush(); |
} |
@@ -100,7 +109,13 @@ |
} |
} |
|
- public List<Integer> match(String pattern, String text) { |
+ /** |
+ * |
+ * @param pattern |
+ * @param text |
+ * @return |
+ */ |
+ public List<Integer> Match(String pattern, String text) { |
List<Integer> matches = new ArrayList<>(); |
int m = text.length(); |
int n = pattern.length(); |
@@ -134,6 +149,11 @@ |
return matches; |
} |
|
+ /** |
+ * |
+ * @param pattern |
+ * @return |
+ */ |
private Map<Character, Integer> preprocessForBadCharacterShift(String pattern) { |
Map<Character, Integer> map = new HashMap<>(); |
for (int i = pattern.length() - 1; i >= 0; i--) { |
@@ -153,11 +173,25 @@ |
} |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param hrus |
+ * @param CDL_years |
+ * @param LMOD_CMZ_Data |
+ * @param DB_LMOD_map |
+ * @param start_year |
+ * @param end_year |
+ * @param w_log |
+ * @return |
+ * @throws InterruptedException |
+ * @throws ExecutionException |
+ */ |
public static HashMap<Integer, ArrayList<String[]>> ConcurrentMatching(File dataDir, List<ERU> hrus, List<String> CDL_years, List<ArrayList<String>> LMOD_CMZ_Data, HashMap<String, List<ArrayList<String>>> DB_LMOD_map, Integer start_year, Integer end_year, PrintWriter w_log) |
throws InterruptedException, ExecutionException { |
|
int threadnr = 1; |
- if (hrus.size() > 8) { |
+ if (hrus.size() > 10) { |
threadnr = CSIP_Const.ThreadNumber; |
} |
|
@@ -210,6 +244,22 @@ |
return result_map; |
} |
|
+ /** |
+ * |
+ * @param first |
+ * @param last |
+ * @param dataDir |
+ * @param hrus |
+ * @param LMOD_CMZ_Data |
+ * @param DB_LMOD_map |
+ * @param years |
+ * @param start_year |
+ * @param end_year |
+ * @param w_log |
+ * @return |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
public static HashMap<Integer, ArrayList<String[]>> Matching(int first, int last, File dataDir, List<ERU> hrus, List<ArrayList<String>> LMOD_CMZ_Data, HashMap<String, List<ArrayList<String>>> DB_LMOD_map, List<String> years, Integer start_year, Integer end_year, PrintWriter w_log) throws ClassNotFoundException, SQLException { |
|
HashMap<Integer, ArrayList<String[]>> map = new HashMap<>(); |
@@ -252,7 +302,7 @@ |
String s1 = matrix[0]; |
String s2 = matrix[1]; |
//String pattern, String text |
- List<Integer> matches = new CSIP_NASS_LMOD_Matching().match(s2, s1); |
+ List<Integer> matches = new CSIP_NASS_LMOD_Matching().Match(s2, s1); |
//int boyer = 1; |
char[] myNameChars = s1.toCharArray(); |
for (Integer integer : matches) { |
@@ -270,7 +320,7 @@ |
String s1 = matrix[0]; |
String s2 = matrix[1]; |
//String pattern, String text |
- List<Integer> matches = new CSIP_NASS_LMOD_Matching().match(s2, s1); |
+ List<Integer> matches = new CSIP_NASS_LMOD_Matching().Match(s2, s1); |
|
char[] myNameChars = s1.toCharArray(); |
for (Integer integer : matches) { |
@@ -297,7 +347,7 @@ |
boolean right_irrigation_rotation = false; |
boolean highest_opposite_irrigation_rotation = false; |
|
- if (hrus.get(i).sorted_adjusted_conf.size() > 1 && hrus.get(i).acc_crops_year_count > 0 && hrus.get(i).non_field_crops_years / hrus.get(i).acc_crops_year_count < 0.51) { |
+ if (hrus.get(i).sorted_adjusted_conf.size() > 1 && hrus.get(i).acc_crops_year_count > 0 && (hrus.get(i).non_field_crops_years / hrus.get(i).acc_crops_year_count) < 0.51) { |
//System.out.println(" Verhaeltnis vegetation & fieldcrops: " + (hrus.get(i).non_field_crops_years / hrus.get(i).acc_crops_year_count) + " non:" + hrus.get(i).non_field_crops_years + " field:" + hrus.get(i).acc_crops_year_count); |
for (String key : hrus.get(i).sorted_adjusted_conf.keySet()) { |
//w_log.println(" " + hash_pos + ".in LIST Seq : " + hrus.get(i).seq_map.get(hrus.get(i).sorted_adjusted_conf.get(key)) + " Adjusted avg. Conf: " + hrus.get(i).sorted_adjusted_conf.get(key) + " for: " + key); |
@@ -335,7 +385,7 @@ |
M_R.rotation_irrigation_correct = right_irrigation_rotation; |
|
//String pattern, String text |
- List<Integer> inner_matches = new CSIP_NASS_LMOD_Matching().match(pattern, hrus.get(i).hru_seq_org); |
+ List<Integer> inner_matches = new CSIP_NASS_LMOD_Matching().Match(pattern, hrus.get(i).hru_seq_org); |
for (Integer inner_integer : inner_matches) { |
|
int ind = 0; |
@@ -406,9 +456,9 @@ |
x++; |
} |
} |
-// w_log.println(" detected rotation :" + org_seq); |
-// w_log.println(" rebuilt rotation :" + new_rebuilt); |
-// w_log.println(" JaroSimilarity : " + jaro.proximity(org_seq, new_rebuilt) + " similar"); |
+ //w_log.println(" detected rotation :" + org_seq); |
+ //w_log.println(" rebuilt rotation :" + new_rebuilt); |
+ //w_log.println(" JaroSimilarity : " + jaro.proximity(org_seq, new_rebuilt) + " similar"); |
if (jaro.proximity(org_seq, new_rebuilt) > jaro_loop_value) { |
jaro_loop_value = jaro.proximity(org_seq, new_rebuilt); |
help = help_inner; |
@@ -435,13 +485,13 @@ |
if (!Char1.equals(Char2)) { |
same = true; |
//String pattern, String text |
- inner_matches = new CSIP_NASS_LMOD_Matching().match(pattern, help); |
+ inner_matches = new CSIP_NASS_LMOD_Matching().Match(pattern, help); |
for (Integer in_integer : inner_matches) { |
M_R.Rot_Start_years = years.get(in_integer); |
break; |
} |
} else { |
- inner_matches = new CSIP_NASS_LMOD_Matching().match(pattern, help); |
+ inner_matches = new CSIP_NASS_LMOD_Matching().Match(pattern, help); |
for (Integer in_integer : inner_matches) { |
M_R.Rot_Start_years = years.get(in_integer); |
break; |
@@ -480,15 +530,24 @@ |
Map<String, Integer> majority_veg = new HashMap<>(); |
String[] each_veg = hrus.get(i).hru_veg_seq.split(";"); |
//for (int j = each_veg.length - 1; j >= ((each_veg.length) - CSIP_Const.ACC_years.length); j--) { |
+ int nodata_counter = 0; |
for (int j = each_veg.length - 1; j >= ((each_veg.length) - each_veg.length); j--) { |
- if (majority_veg.get(each_veg[j]) == null) { |
- majority_veg.put(each_veg[j], 1); |
+ if (!each_veg[j].equals("NODATA")) { |
+ if (majority_veg.get(each_veg[j]) == null) { |
+ majority_veg.put(each_veg[j], 1); |
+ } else { |
+ int count = majority_veg.get(each_veg[j]); |
+ majority_veg.put(each_veg[j], count + 1); |
+ } |
} else { |
- int count = majority_veg.get(each_veg[j]); |
- majority_veg.put(each_veg[j], count + 1); |
+ nodata_counter++; |
} |
} |
majority_veg = sortByValues(majority_veg); |
+ if (majority_veg.isEmpty()) { |
+ majority_veg.put("NODATA", 1); |
+ } |
+ |
for (String key : majority_veg.keySet()) { |
//w_log.println(" =====>>>> Majority vegetation: " + key + " , count = " + majority_veg.get(key)); |
M_R.Rotation = false; |
@@ -655,6 +714,12 @@ |
empty = false; |
} |
} |
+ if (empty) { |
+ w_log.println(" BIIGG ERROR with HRU: " + hrus.get(i).ID + " and sequence: " + hrus.get(i).hru_seq_work); |
+ w_log.flush(); |
+ |
+ } |
+ |
} |
conn.close();//end db connection |
} |
@@ -676,118 +741,15 @@ |
return map; |
} |
|
- public static void NASS_LMOD_perfect_data_writer(File workspace, File dataDir, List<ERU> hrus, HashMap<Integer, ArrayList<String[]>> map, PrintWriter w_log) throws IOException, URISyntaxException, SQLException, ClassNotFoundException { |
- |
- ArrayList<String> Unique_LMOD_KEYs = new ArrayList<>(); |
- for (int i = 0; i < hrus.size(); i++) { |
- hrus.get(i).Year_LMOD_Rot_Linkage = map.get(i); |
- w_log.println("->[" + hrus.get(i).ID + "] irrigated: " + hrus.get(i).irrigated); |
- //w_log.println(" " + hrus.get(i).hru_veg_seq); |
- if (hrus.get(i).Matching_Result.Rotation) { |
- w_log.println(" " + hrus.get(i).Matching_Result.CropName_Sequence); |
- w_log.println(" " + hrus.get(i).Matching_Result.LMOD_Key); |
- } else { |
- w_log.println(" " + hrus.get(i).Matching_Result.Vegetation_Sequence); |
- } |
- for (int j = 0; j < hrus.get(i).Year_LMOD_Rot_Linkage.size(); j++) { |
- //w_log.println(" " + hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[1] + " " + hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]); |
- String KeyTest = hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]; |
- char Chars = KeyTest.charAt(0); |
- if (Chars >= '0' && Chars <= '9') { |
- |
- if (!Unique_LMOD_KEYs.contains(hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0])) { |
- Unique_LMOD_KEYs.add(hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]); |
- } |
- } |
- } |
- w_log.println(" "); |
- w_log.flush(); |
- } |
- |
-// w_log.println(Unique_LMOD_KEYs.size() + " different managements found."); |
- HashMap<String, ArrayList<String[]>> DB_map = new HashMap<>(); |
- |
- for (int i = 0; i < Unique_LMOD_KEYs.size(); i++) { |
- Class.forName("org.h2.Driver"); |
- try (Connection conn = DriverManager.getConnection("jdbc:h2:file:" + dataDir.getPath() + CSIP_Const.LocalDB, "sa", "")) { |
- Statement state = conn.createStatement(); |
- String queryString = "select distinct * from \"PUBLIC\".LMOD_MANAGEMENTS where MANKEY='" + Unique_LMOD_KEYs.get(i) + "' order by date"; |
- ResultSet result = state.executeQuery(queryString); |
- ArrayList<String[]> Management_data = new ArrayList<>(); |
- while (result.next()) { |
- String[] DBrow = new String[4]; |
- DBrow[0] = result.getString("OPKEY").trim(); |
- DBrow[1] = result.getString("DATE").trim(); |
- String Seed_Harv = ""; |
- if (result.getString("SEED_HARV") != null) { |
- Seed_Harv = result.getString("SEED_HARV").trim(); |
- } |
- DBrow[2] = Seed_Harv; |
- String Veg = ""; |
- if (result.getString("VEGETATION") != null) { |
- Veg = result.getString("VEGETATION").trim(); |
- } |
- DBrow[3] = Veg; |
- Management_data.add(DBrow); |
- } |
- DB_map.put(Unique_LMOD_KEYs.get(i), Management_data); |
- state.close(); |
- conn.close();//end db connection |
- } |
- |
- } |
- |
- PrintWriter w = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_NASS_LMOD_result_perf))); |
- |
- w.println("PolygonID, NASS-Year, Date, Seed/Harvest, Vegetation "); |
- |
- for (int i = 0; i < hrus.size(); i++) { |
- |
- for (int j = 0; j < hrus.get(i).Year_LMOD_Rot_Linkage.size(); j++) { |
- |
- int year = Integer.parseInt(hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[1]); |
- |
- if (DB_map.get(hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]) != null) { |
- w_log.println(" " + hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]); |
- w_log.println(" "); |
- w_log.flush(); |
- ArrayList<String[]> Management = DB_map.get(hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]); |
- |
- int help = 0; |
- if (Management.get(0)[1].replaceAll("\\.", "").equals("00001101")) { |
- help = 1; |
- } |
- |
- String[] date_parts = Management.get(help)[1].split("\\."); |
- |
- int old_year = Integer.parseInt(date_parts[0]); |
- |
- for (int k = help; k < Management.size(); k++) { |
- |
- date_parts = Management.get(k)[1].split("\\."); |
- |
-//// w_log.println(" --> " + hrus.get(i).ID + " , " + year + "/" + date_parts + ", " + Management.get(k)[2] + ", " + Management.get(k)[3]); |
-//// w_log.flush(); |
- int new_year = Integer.parseInt(date_parts[0]); |
- |
- if (new_year > old_year) { |
- year = year + 1; |
- old_year = new_year; |
- } |
-//// w_log.println(" " + hrus.get(i).ID + " , " + year + "/" + date_parts[1] + "/" + date_parts[2] + ", " + Management.get(k)[2] + ", " + Management.get(k)[3]); |
-//// w_log.flush(); |
- w.println(hrus.get(i).ID + " , " + year + " , " + date_parts[1] + "/" + date_parts[2] + ", " + Management.get(k)[2] + ", " + Management.get(k)[3]); |
- |
- } |
- } else { |
- w.println(hrus.get(i).ID + " , " + year + ", - , - ," + hrus.get(i).Year_LMOD_Rot_Linkage.get(j)[0]); |
- } |
- } |
- } |
- |
- w.close(); |
- } |
- |
+ /** |
+ * |
+ * @param workspace |
+ * @param hrus |
+ * @param irrigation_map |
+ * @param delta |
+ * @param w_log |
+ * @throws IOException |
+ */ |
public static void AOI_NASS_LMOD_generic_match_data_writer(File workspace, List<ERU> hrus, HashMap<Integer, ArrayList<HashMap<Integer, Double>>> irrigation_map, double delta, PrintWriter w_log) throws IOException { |
|
PrintWriter w = new PrintWriter(new FileWriter(new File(workspace, CSIP_Const.AOI_NASS_LMOD_generic_result))); |
@@ -833,6 +795,24 @@ |
w.close(); |
} |
|
+ /** |
+ * |
+ * @param dataDir |
+ * @param hrus |
+ * @param DB_map_managements |
+ * @param Unique_LMOD_OPKEYs |
+ * @param Unique_LMOD_ManKEYs |
+ * @param managements |
+ * @param OP_map |
+ * @param DB_map_managements_acr |
+ * @param OP_map_tillage |
+ * @param LMOD_AGES_CID_map |
+ * @param only_veg_map |
+ * @param w_log |
+ * @throws IOException |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
public static void AOI_NASS_DETECTED_LMODDATA_PREPARATION(File dataDir, List<ERU> hrus, HashMap<String, ArrayList<String[]>> DB_map_managements, ArrayList<String> Unique_LMOD_OPKEYs, ArrayList<String> Unique_LMOD_ManKEYs, HashMap<String, HashMap<Integer, ArrayList<String[]>>> managements, HashMap<String, String> OP_map, HashMap<String, String[]> DB_map_managements_acr, HashMap<String, String[]> OP_map_tillage, HashMap<String, String> LMOD_AGES_CID_map, HashMap<String, String> only_veg_map, PrintWriter w_log) throws IOException, ClassNotFoundException, SQLException { |
|
ArrayList<String[]> custom_line = new ArrayList<>(); |
@@ -855,9 +835,9 @@ |
ResultSet result = state.executeQuery(queryString); |
while (result.next()) { |
String[] LMOD_names = result.getString("LAMS_LMOD_H2").trim().split(";"); |
- for (int i = 0; i < LMOD_names.length; i++) { |
- if (LMOD_AGES_CID_map.get(LMOD_names[i]) == null) { |
- LMOD_AGES_CID_map.put(LMOD_names[i], result.getString("AGES_CID").trim().split(";")[0]); |
+ for (String LMOD_name : LMOD_names) { |
+ if (LMOD_AGES_CID_map.get(LMOD_name) == null) { |
+ LMOD_AGES_CID_map.put(LMOD_name, result.getString("AGES_CID").trim().split(";")[0]); |
} |
} |
} |
@@ -970,6 +950,23 @@ |
} |
} |
|
+ /** |
+ * |
+ * @param workspace |
+ * @param hrus |
+ * @param DB_map_managements |
+ * @param managements |
+ * @param OP_map |
+ * @param DB_map_managements_acr |
+ * @param LMOD_AGES_MID_map |
+ * @param only_veg_map |
+ * @param start_year |
+ * @param end_year |
+ * @param w_log |
+ * @throws IOException |
+ * @throws ClassNotFoundException |
+ * @throws SQLException |
+ */ |
public static void AOI_NASS_LMODDATA_costum_generic_writer(File workspace, List<ERU> hrus, HashMap<String, ArrayList<String[]>> DB_map_managements, HashMap<String, HashMap<Integer, ArrayList<String[]>>> managements, HashMap<String, String> OP_map, HashMap<String, String[]> DB_map_managements_acr, HashMap<String, String> LMOD_AGES_MID_map, HashMap<String, String> only_veg_map, Integer start_year, Integer end_year, PrintWriter w_log) throws IOException, ClassNotFoundException, SQLException { |
|
List<String> records = new ArrayList<>(); |
@@ -1171,6 +1168,10 @@ |
if (LMOD_AGES_MID_map.get(only_veg_map.get(hrus.get(i).Matching_Result.Vegetation_Sequence)) == null) { |
LMOD_AGES_MID_map.put(only_veg_map.get(hrus.get(i).Matching_Result.Vegetation_Sequence), only_veg_map.get(hrus.get(i).Matching_Result.Vegetation_Sequence)); |
} |
+ if (only_veg_map.get(hrus.get(i).Matching_Result.Vegetation_Sequence) == null) { |
+ System.out.println(hrus.get(i).ID + " !!!!! only veg with -NULL- " + hrus.get(i).Matching_Result.Vegetation_Sequence); |
+ |
+ } |
for (int j = start_year; j < end_year + 1; j++) { |
records.add(hrus.get(i).ID + " , 01/01/" + j + " ,NA, " + hrus.get(i).Matching_Result.Vegetation_Sequence + ",NA,NA,NA,NA,NA\n"); |
records.add(hrus.get(i).ID + " , 12/31/" + j + " ,NA, " + hrus.get(i).Matching_Result.Vegetation_Sequence + ",NA,NA,NA,NA,NA\n"); |