Skip to content

Commit 60e2566

Browse files
committed
new package: ohdm2geoserverrendering
- added OHDM2Geoserverrendering class
1 parent 660b5aa commit 60e2566

File tree

1 file changed

+326
-0
lines changed

1 file changed

+326
-0
lines changed
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
package ohdm2geoserverrendering;
2+
3+
import util.DB;
4+
import util.Parameter;
5+
import util.SQLStatementQueue;
6+
7+
import java.io.*;
8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
import java.util.ArrayList;
11+
import java.util.Enumeration;
12+
import java.util.List;
13+
import java.util.jar.JarEntry;
14+
import java.util.jar.JarFile;
15+
16+
public class OHDM2Geoserverrendering {
17+
18+
public List<String> defaultSQLStatementList = new ArrayList<>();
19+
public List<String> actualSQLStatementList = new ArrayList<>();
20+
21+
String admin_labels_sql = "";
22+
String amenities_sql = "";
23+
String boundaries_sql = "";
24+
String buildings_sql = "";
25+
String housenumbers_sql = "";
26+
String landusages_sql = "";
27+
String places_sql = "";
28+
String roads_sql = "";
29+
String transport_areas_sql = "";
30+
String transport_points_sql = "";
31+
String waterarea_sql = "";
32+
String waterways_sql = "";
33+
34+
public static void main(String[] args) throws SQLException, IOException {
35+
String sourceParameterFileName = "db_ohdm.txt";
36+
String targetParameterFileName = "db_rendering.txt";
37+
38+
39+
if(args.length > 0) {
40+
sourceParameterFileName = args[0];
41+
}
42+
43+
if(args.length > 1) {
44+
targetParameterFileName = args[1];
45+
}
46+
47+
// Connection sourceConnection = Importer.createLocalTestSourceConnection();
48+
// Connection targetConnection = Importer.createLocalTestTargetConnection();
49+
50+
Parameter sourceParameter = new Parameter(sourceParameterFileName);
51+
Parameter targetParameter = new Parameter(targetParameterFileName);
52+
53+
Connection connection = DB.createConnection(targetParameter);
54+
55+
String targetSchema = targetParameter.getSchema();
56+
57+
String sourceSchema = sourceParameter.getSchema();
58+
59+
SQLStatementQueue sql = new SQLStatementQueue(connection);
60+
OHDM2Geoserverrendering renderer = new OHDM2Geoserverrendering();
61+
62+
renderer.loadSQLFiles();
63+
renderer.changeDefaultParametersToActual(targetSchema, sourceSchema);
64+
65+
66+
renderer.executeSQLStatements(sql);
67+
68+
System.out.println("Render tables creation for Geoserver finished");
69+
70+
System.out.println("Start copying symbols into user-dir..");
71+
renderer.loadSymbolsFromResources();
72+
System.out.println("Start copying css-files into user-dir..");
73+
renderer.loadCssFromResourecs();
74+
75+
if(!renderer.checkFiles()){
76+
System.out.println("CSS and/or symbolfiles couldnt be created successfully.\n" +
77+
"Please download these files manually from: https://github.com/teceP/OSMImportUpdateGeoserverResources");
78+
}else{
79+
System.out.println("CSS and symbolfiles has been created successfully.");
80+
}
81+
82+
}
83+
84+
void loadSQLFiles(){
85+
System.out.println("Load SQL-Files...");
86+
87+
admin_labels_sql = loadSqlFromResources("resources/sqls/admin_labels.sql");
88+
amenities_sql = loadSqlFromResources("resources/sqls/amenities.sql");
89+
boundaries_sql = loadSqlFromResources("resources/sqls/boundaries.sql");
90+
buildings_sql = loadSqlFromResources("resources/sqls/buildings.sql");
91+
housenumbers_sql = loadSqlFromResources("resources/sqls/housenumbers.sql");
92+
landusages_sql = loadSqlFromResources("resources/sqls/landusages.sql");
93+
places_sql = loadSqlFromResources("resources/sqls/places.sql");
94+
roads_sql = loadSqlFromResources("resources/sqls/roads.sql");
95+
transport_areas_sql = loadSqlFromResources("resources/sqls/transport_areas.sql");
96+
transport_points_sql = loadSqlFromResources("resources/sqls/transport_points.sql");
97+
waterarea_sql = loadSqlFromResources("resources/sqls/waterarea.sql");
98+
waterways_sql = loadSqlFromResources("resources/sqls/waterways.sql");
99+
100+
defaultSQLStatementList.add(admin_labels_sql);
101+
defaultSQLStatementList.add(amenities_sql);
102+
defaultSQLStatementList.add(boundaries_sql);
103+
defaultSQLStatementList.add(buildings_sql);
104+
defaultSQLStatementList.add(housenumbers_sql);
105+
defaultSQLStatementList.add(landusages_sql);
106+
defaultSQLStatementList.add(places_sql);
107+
defaultSQLStatementList.add(roads_sql);
108+
defaultSQLStatementList.add(transport_areas_sql);
109+
defaultSQLStatementList.add(transport_points_sql);
110+
defaultSQLStatementList.add(waterarea_sql);
111+
defaultSQLStatementList.add(waterways_sql);
112+
}
113+
114+
void changeDefaultParametersToActual(String targetSchema, String sourceSchema){
115+
for (String statement : defaultSQLStatementList) {
116+
117+
String actualStatement = statement.replaceAll("my_test_schema", targetSchema);
118+
actualStatement = actualStatement.replaceAll("ohdm", sourceSchema);
119+
120+
actualSQLStatementList.add(actualStatement);
121+
}
122+
}
123+
124+
void executeSQLStatements(SQLStatementQueue sql){
125+
126+
System.out.println(actualSQLStatementList.size() + " statements in queue. Start execute SQL files... ");
127+
128+
float eachPercentage = 100/actualSQLStatementList.size();
129+
float currentPercentage = 0;
130+
131+
for(String statement: actualSQLStatementList){
132+
sql.append(statement);
133+
try {
134+
sql.forceExecute();
135+
} catch (SQLException e) {
136+
e.printStackTrace();
137+
}
138+
sql.resetStatement();
139+
currentPercentage = currentPercentage + eachPercentage;
140+
System.out.println(currentPercentage + " % finished.");
141+
}
142+
143+
144+
System.out.println("100 % finished.");
145+
146+
}
147+
148+
public void deleteOldDatas(File dir){
149+
150+
if(dir.exists()){
151+
//clean up
152+
if(dir.isDirectory()){
153+
String[] files = dir.list();
154+
for(String file : files){
155+
File currentFile = new File(file);
156+
currentFile.delete();
157+
}
158+
}
159+
}
160+
161+
}
162+
163+
public String loadSqlFromResources(String path){
164+
InputStream in = getClass().getResourceAsStream(path);
165+
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
166+
String content = "";
167+
try {
168+
content = readFile(reader);
169+
} catch (IOException e) {
170+
e.printStackTrace();
171+
}
172+
173+
System.out.println("File loaded from: " + path);
174+
return content;
175+
}
176+
177+
178+
public String readFile(BufferedReader reader) throws IOException {
179+
String content = "";
180+
String line = reader.readLine();
181+
182+
while(line != null){
183+
content = content + " " + line + System.lineSeparator();
184+
line = reader.readLine();
185+
}
186+
187+
return content;
188+
}
189+
190+
/**
191+
* CSS
192+
*/
193+
194+
void loadCssFromResourecs(){
195+
File targetdir = new File("css");
196+
197+
this.deleteOldDatas(targetdir);
198+
199+
targetdir.mkdir();
200+
201+
JarFile jar = null;
202+
String path = "resources/css/";
203+
try {
204+
jar = new JarFile("OSMImportUpdate.jar");
205+
Enumeration<JarEntry> entries = jar.entries();
206+
while (entries.hasMoreElements()) {
207+
JarEntry entry = entries.nextElement();
208+
209+
if (entry.getName().contains(path) && !entry.isDirectory()) {
210+
System.out.println("Load symbol: " + entry.getName());
211+
createCssFromResources("/" + entry.getName());
212+
}
213+
}
214+
} catch (IOException e) {
215+
e.printStackTrace();
216+
}
217+
}
218+
219+
void createCssFromResources(String filename){
220+
221+
try {
222+
InputStream in = getClass().getResourceAsStream(filename);
223+
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
224+
225+
String content = readFile(reader);
226+
227+
int pos = filename.length()-1;
228+
229+
while(filename.charAt(pos) != '/'){
230+
pos--;
231+
}
232+
233+
filename = filename.substring(pos);
234+
235+
File file = new File("css" + filename);
236+
file.createNewFile();
237+
238+
BufferedWriter writer = new BufferedWriter(new FileWriter(file)); //write file (content)
239+
writer.write(content);
240+
writer.close();
241+
242+
} catch (IOException e) {
243+
e.printStackTrace();
244+
}
245+
}
246+
247+
/**
248+
* Symbols
249+
*/
250+
251+
public void loadSymbolsFromResources(){
252+
253+
System.out.println("Symbol files will be copied to working dir..(" + System.getProperty("user.dir") + "/symbols/ )");
254+
255+
File targetdir = new File("symbols");
256+
257+
this.deleteOldDatas(targetdir);
258+
259+
targetdir.mkdir();
260+
261+
JarFile jar = null;
262+
String path = "resources/symbols/";
263+
try {
264+
jar = new JarFile("OSMImportUpdate.jar");
265+
Enumeration<JarEntry> entries = jar.entries();
266+
while (entries.hasMoreElements()) {
267+
JarEntry entry = entries.nextElement();
268+
269+
if (entry.getName().contains(path) && !entry.isDirectory()) {
270+
System.out.println("Load symbol: " + entry.getName());
271+
createSymbolsFromResources("/" + entry.getName());
272+
}
273+
}
274+
} catch (IOException e) {
275+
e.printStackTrace();
276+
}
277+
}
278+
279+
public void createSymbolsFromResources(String filename){
280+
281+
//There are some problems with writing and reading the SVG files..
282+
//Methode shouldnt be used till its working.. Download symbols from git repo (link in main-method)
283+
284+
/* try {
285+
BufferedImage bimage = ImageIO.read(getClass().getResource(filename));
286+
287+
int pos = filename.length()-1;
288+
289+
while(filename.charAt(pos) != '/'){
290+
pos--;
291+
}
292+
293+
filename = filename.substring(pos);
294+
295+
File file = new File("symbols" + filename);
296+
file.createNewFile();
297+
ImageIO.write(bimage, "png", file);
298+
299+
} catch (IOException e) {
300+
// e.printStackTrace();
301+
}*/
302+
}
303+
304+
public boolean checkFiles(){
305+
File symbols = new File("symbols");
306+
boolean symbolFiles = false;
307+
308+
if(symbols.isDirectory()){
309+
if(symbols.listFiles().length > 0){
310+
symbolFiles = true;
311+
}
312+
}
313+
314+
File css = new File("css");
315+
boolean cssFiles = false;
316+
317+
if(css.isDirectory()){
318+
if(css.listFiles().length > 0){
319+
cssFiles = true;
320+
}
321+
}
322+
323+
return symbolFiles && cssFiles;
324+
}
325+
326+
}

0 commit comments

Comments
 (0)