]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.gis/src/main/java/org/argeo/slc/postgis/ImportShapefile.java
Improve GPX importer
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.gis / src / main / java / org / argeo / slc / postgis / ImportShapefile.java
1 package org.argeo.slc.postgis;
2
3 import java.io.File;
4
5 import org.apache.commons.io.FilenameUtils;
6 import org.argeo.slc.SlcException;
7 import org.argeo.slc.core.execution.tasks.SystemCall;
8 import org.argeo.slc.gdal.OgrProcess;
9 import org.springframework.core.io.FileSystemResource;
10
11 public class ImportShapefile extends OgrProcess {
12
13 private Boolean createIndex = true;
14 private String mode = "-d";
15 private String tableName;
16 private String dbHost = "localhost";
17 private Integer dbPort = 5432;
18 private String dbName;
19 private String dbUser;
20
21 @Override
22 public void run() {
23 try {
24 File shapefile;
25 if (runOgr2Ogr()) {
26 shapefile = File.createTempFile(
27 FilenameUtils.getBaseName(getSource().getName()),
28 FilenameUtils.getExtension(getSource().getName()));
29 setTarget(shapefile);
30 // do OGR pre-processing
31 super.run();
32 } else {
33 shapefile = getSource();
34 }
35
36 if (tableName == null)
37 tableName = FilenameUtils.getBaseName(getSource().getName());
38
39 SystemCall shp2pgsql = new SystemCall("shp2pgsql");
40 if (createIndex)
41 shp2pgsql.arg("-I");
42 if (getTargetSrs() != null) {
43 if (getTargetSrs().toUpperCase().startsWith("EPSG:")) {
44 String srid = getTargetSrs().toUpperCase().substring(
45 "EPSG:".length());
46 shp2pgsql.arg("-s", srid);
47 } else {
48 throw new SlcException("Cannot interpret SRS "
49 + getTargetSrs());
50 }
51 }
52 shp2pgsql.arg(mode);
53 shp2pgsql.arg(tableName);
54 shp2pgsql.arg(shapefile.getAbsolutePath());
55
56 // TODO use pipes
57 File sqlOutput = File.createTempFile(
58 "shp2psql-"
59 + FilenameUtils.getBaseName(getSource().getName()),
60 ".sql");
61 shp2pgsql.setStdOutFile(new FileSystemResource(sqlOutput));
62 shp2pgsql.setRedirectStdOut(true);
63
64 shp2pgsql.run();
65
66 SystemCall psql = new SystemCall("psql");
67 psql.arg("-h", dbHost);
68 psql.arg("-p", dbPort.toString());
69 psql.arg("-d", dbName);
70 psql.arg("-U", dbUser);
71 psql.setStdInFile(new FileSystemResource(sqlOutput));
72 psql.run();
73
74 } catch (Exception e) {
75 throw new SlcException("Cannot import " + getSource(), e);
76 }
77 }
78
79 public void setCreateIndex(Boolean createIndex) {
80 this.createIndex = createIndex;
81 }
82
83 public void setMode(String mode) {
84 this.mode = mode;
85 }
86
87 public void setTableName(String tableName) {
88 this.tableName = tableName;
89 }
90
91 public void setDbHost(String dbHost) {
92 this.dbHost = dbHost;
93 }
94
95 public void setDbPort(Integer dbPort) {
96 this.dbPort = dbPort;
97 }
98
99 public void setDbName(String dbName) {
100 this.dbName = dbName;
101 }
102
103 public void setDbUser(String dbUser) {
104 this.dbUser = dbUser;
105 }
106
107 }