Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/utils/RiverMapfileGenerator.java @ 5379:61bf64b102bc mapgenfix
Merge with default branch
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Fri, 22 Mar 2013 11:25:54 +0100 |
parents | b55975761708 |
children | ae60bb7b8085 |
comparison
equal
deleted
inserted
replaced
5175:cfc5540a4eec | 5379:61bf64b102bc |
---|---|
1 package de.intevation.flys.utils; | |
2 | |
3 import com.vividsolutions.jts.geom.Envelope; | |
4 import com.vividsolutions.jts.geom.MultiLineString; | |
5 | |
6 import de.intevation.flys.artifacts.model.LayerInfo; | |
7 import de.intevation.flys.artifacts.model.RiverFactory; | |
8 | |
9 import de.intevation.flys.model.River; | |
10 import de.intevation.flys.model.RiverAxis; | |
11 | |
12 import java.io.File; | |
13 import java.io.FileNotFoundException; | |
14 | |
15 import java.util.ArrayList; | |
16 import java.util.List; | |
17 | |
18 import java.util.regex.Pattern; | |
19 | |
20 import org.apache.log4j.Logger; | |
21 | |
22 import org.apache.velocity.Template; | |
23 import org.hibernate.HibernateException; | |
24 | |
25 public class RiverMapfileGenerator extends MapfileGenerator { | |
26 | |
27 public static final String XPATH_RIVERMAP_RIVER_PROJECTION = | |
28 "/artifact-database/rivermap/river[@name=$name]/srid/@value"; | |
29 | |
30 public static final String XPATH_RIVERMAP_SHAPEFILE_DIR = | |
31 "/artifact-database/rivermap/shapefile-path/@value"; | |
32 | |
33 public static final String XPATH_RIVERMAP_VELOCITY_LOGFILE = | |
34 "/artifact-database/rivermap/velocity/logfile/@path"; | |
35 | |
36 public static final String XPATH_RIVERMAP_MAPSERVER_URL = | |
37 "/artifact-database/rivermap/mapserver/server/@path"; | |
38 | |
39 public static final String XPATH_RIVERMAP_MAPFILE_PATH = | |
40 "/artifact-database/rivermap/mapserver/mapfile/@path"; | |
41 | |
42 public static final String XPATH_RIVERMAP_MAPFILE_TEMPLATE = | |
43 "/artifact-database/rivermap/mapserver/map-template/@path"; | |
44 | |
45 public static final String XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH = | |
46 "/artifact-database/rivermap/mapserver/templates/@path"; | |
47 | |
48 public static final Pattern DB_URL_PATTERN = | |
49 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)"); | |
50 | |
51 public static final Pattern DB_PSQL_URL_PATTERN = | |
52 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z0-9]+)"); | |
53 | |
54 private static Logger logger = Logger.getLogger(RiverMapfileGenerator.class); | |
55 | |
56 /** | |
57 * Generate river axis mapfile. | |
58 */ | |
59 @Override | |
60 public void generate() { | |
61 logger.debug("generate()"); | |
62 | |
63 List<River> rivers = RiverFactory.getRivers(); | |
64 List<String> riverFiles = new ArrayList<String>(); | |
65 | |
66 for (River river : rivers) { | |
67 // We expect that every river has only one RiverAxis. | |
68 // This is not correct but currently the case here, see | |
69 // RiverAxis.java. | |
70 List<RiverAxis> riverAxis = null; | |
71 try { | |
72 riverAxis = RiverAxis.getRiverAxis(river.getName()); | |
73 } | |
74 catch (HibernateException iae) { | |
75 logger.error("No valid riveraxis found for " + river.getName()); | |
76 continue; | |
77 } | |
78 | |
79 if (riverAxis == null) { | |
80 logger.warn("River " + river.getName() + " has no river axis!"); | |
81 continue; | |
82 } | |
83 if (riverAxis.get(0).getGeom() == null) { | |
84 logger.warn("River " + river.getName() + | |
85 " has no riveraxis geometry!"); | |
86 continue; | |
87 } | |
88 MultiLineString geom = riverAxis.get(0).getGeom(); | |
89 Envelope extent = geom.getEnvelopeInternal(); | |
90 | |
91 createRiverAxisLayer( | |
92 river.getName(), | |
93 river.getId(), | |
94 Integer.toString(geom.getSRID()), | |
95 extent.getMinX() + " " + | |
96 extent.getMinY() + " " + | |
97 extent.getMaxX() + " " + | |
98 extent.getMaxY()); | |
99 | |
100 riverFiles.add("river-" + river.getName() + ".map"); | |
101 } | |
102 writeMapfile(riverFiles); | |
103 } | |
104 | |
105 protected void createRiverAxisLayer(String riverName, int riverID, String srid, String extend) { | |
106 LayerInfo layerInfo = new LayerInfo(); | |
107 layerInfo.setName(riverName); | |
108 layerInfo.setConnection(MapUtils.getConnection()); | |
109 layerInfo.setConnectionType(MapUtils.getConnectionType()); | |
110 layerInfo.setSrid(srid); | |
111 layerInfo.setExtent(extend); | |
112 layerInfo.setType("line"); | |
113 // FIXME: Use templates for that | |
114 if (FLYSUtils.isUsingOracle()) { | |
115 layerInfo.setData("geom FROM river_axes USING SRID " + srid); | |
116 } else { | |
117 layerInfo.setData("geom FROM river_axes"); | |
118 } | |
119 layerInfo.setFilter("river_id = " + riverID); | |
120 layerInfo.setTitle(riverName + " RiverAxis"); | |
121 | |
122 File layerFile = new File("river-" + riverName + ".map"); | |
123 Template template = getTemplateByName("riveraxis-layer.vm"); | |
124 if (template == null) { | |
125 logger.warn("Template riveraxis-layer.vm not found."); | |
126 return; | |
127 } | |
128 | |
129 try { | |
130 writeLayer(layerInfo, layerFile, template); | |
131 } | |
132 catch (FileNotFoundException e) { | |
133 logger.warn(e.getLocalizedMessage(), e); | |
134 } | |
135 } | |
136 | |
137 @Override | |
138 protected String getVelocityLogfile() { | |
139 return FLYSUtils.getXPathString(XPATH_RIVERMAP_VELOCITY_LOGFILE); | |
140 } | |
141 | |
142 @Override | |
143 protected String getMapserverTemplatePath() { | |
144 return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_TEMPLATE_PATH); | |
145 } | |
146 | |
147 @Override | |
148 public String getMapserverUrl() { | |
149 return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPSERVER_URL); | |
150 } | |
151 | |
152 @Override | |
153 protected String getMapfilePath() { | |
154 return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_PATH); | |
155 } | |
156 | |
157 @Override | |
158 protected String getMapfileTemplate() { | |
159 return FLYSUtils.getXPathString(XPATH_RIVERMAP_MAPFILE_TEMPLATE); | |
160 } | |
161 } |