changeset 5027:25c180fa6162

Use BWASTRParser to set official numbers on rivers.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Tue, 19 Feb 2013 16:41:00 +0100
parents bf22086747bc
children d1b50434b259
files flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/Importer.java
diffstat 2 files changed, 63 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Feb 19 12:03:55 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Feb 19 16:41:00 2013 +0100
@@ -297,6 +297,7 @@
         parseWaterlevelDifferences();
         parseMeasurementStations();
         parseSQRelation();
+        storeOfficialNumber();
     }
 
     public void parseFloodProtection() throws IOException {
@@ -341,6 +342,13 @@
         }
     }
 
+    public void storeOfficialNumber() {
+        if (Config.INSTANCE.skipBWASTR()) {
+            log.info("skip storing official number.");
+            return;
+        }
+        getPeer().setOfficialNumber(officialNumber);
+    }
 
     public void parseBedHeight() throws IOException {
         File minfoDir     = getMinfoDir();
@@ -1447,13 +1455,19 @@
             Session session = ImporterSession.getInstance().getDatabaseSession();
             Query query = session.createQuery("from River where name=:name");
 
-            Unit u = wstUnit.getPeer();
+            Unit u = null;
+            if (wstUnit != null) {
+                u = wstUnit.getPeer();
+            }
 
             query.setString("name", name);
             List<River> rivers = query.list();
             if (rivers.isEmpty()) {
                 log.info("Store new river '" + name + "'");
                 peer = new River(name, u);
+                if (!Config.INSTANCE.skipBWASTR()) {
+                    peer.setOfficialNumber(officialNumber);
+                }
                 session.save(peer);
             }
             else {
--- a/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Tue Feb 19 12:03:55 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/Importer.java	Tue Feb 19 16:41:00 2013 +0100
@@ -2,13 +2,16 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.flys.importer.parsers.AnnotationClassifier;
+import de.intevation.flys.importer.parsers.BundesWasserStrassenParser;
 import de.intevation.flys.importer.parsers.InfoGewParser;
-import de.intevation.flys.importer.parsers.AnnotationClassifier;
 
 import java.io.File;
 import java.io.IOException;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import java.sql.SQLException;
 
@@ -19,6 +22,7 @@
 
 import org.w3c.dom.Document;
 
+import de.intevation.flys.utils.StringUtil;
 
 /** Data Importer. Further processing happens per-river. */
 public class Importer
@@ -26,6 +30,8 @@
     /** Private logger. */
     private static Logger log = Logger.getLogger(Importer.class);
 
+    private static String BWASTR_ID_CSV_FILE = "BWASTR_ID.csv";
+
     protected List<ImportRiver> rivers;
 
     public Importer() {
@@ -43,6 +49,7 @@
         this.rivers = rivers;
     }
 
+    /** Write rivers and their dependencies/dependants to db. */
     public void writeRivers() {
         log.debug("write rivers started");
 
@@ -122,10 +129,16 @@
 
         log.info("Start parsing rivers...");
 
+        File bwastrFile = null;
+
         for (String gew: args) {
             log.info("parsing info gew file: " + gew);
+            File gewFile = new File(gew);
+            if (bwastrFile == null) {
+                bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
+            }
             try {
-                infoGewParser.parse(new File(gew));
+                infoGewParser.parse(gewFile);
             }
             catch (IOException ioe) {
                 log.error("error while parsing gew: " + gew);
@@ -135,14 +148,46 @@
         String gew = Config.INSTANCE.getInfoGewFile();
         if (gew != null && gew.length() > 0) {
             log.info("parsing info gew file: " + gew);
+            File gewFile = new File(gew);
+            if (bwastrFile == null) {
+                bwastrFile = new File(gewFile.getParentFile(), BWASTR_ID_CSV_FILE);
+            }
             try {
-                infoGewParser.parse(new File(gew));
+                infoGewParser.parse(gewFile);
             }
             catch (IOException ioe) {
                 log.error("error while parsing gew: " + gew);
             }
         }
 
+        // Look for official numbers.
+        BundesWasserStrassenParser bwastrIdParser =
+            new BundesWasserStrassenParser();
+
+        // Read bwastFile (river-dir + BWASTR_ID_CSV_FILE).
+        if (!Config.INSTANCE.skipBWASTR()) {
+            try{
+                bwastrIdParser.parse(bwastrFile);
+                HashMap<String,Long> map = bwastrIdParser.getMap();
+
+                // Now link rivers with official numbers.
+                for(ImportRiver river: infoGewParser.getRivers()) {
+                    for(Map.Entry<String, Long> entry: map.entrySet()) {
+                        if (StringUtil.containsIgnoreCase(entry.getKey(), river.getName())) {
+                            river.setOfficialNumber(entry.getValue());
+                            river.storeOfficialNumber();
+                            log.debug(river.getName() + " is mapped to bwastr " + entry.getValue());
+                        }
+                    }
+                }
+            } catch (IOException ioe) {
+                log.warn("BWASTR-file could not be loaded.");
+            }
+        }
+        else {
+            log.debug("skip reading BWASTR_ID.csv");
+        }
+
         if (!Config.INSTANCE.dryRun()) {
             new Importer(infoGewParser.getRivers()).writeToDatabase();
         }

http://dive4elements.wald.intevation.org