-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bacd0a6
Showing
3 changed files
with
259 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/target/ | ||
/.settings | ||
.classpath | ||
.project | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>fr.ign.simplu3d.tools</groupId> | ||
<artifactId>simplushp2pgsql</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>simplushp2pgsql</name> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<geoxcore.version>1.9-SNAPSHOT</geoxcore.version> | ||
</properties> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.1</version> | ||
<configuration> | ||
<compilerVersion>1.8</compilerVersion> | ||
<source>1.8</source> | ||
<complianceLevel>1.8</complianceLevel> | ||
<target>1.8</target> | ||
<includes> | ||
<include>**/*</include> | ||
</includes> | ||
<excludes> | ||
</excludes> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<dependencies> | ||
<!-- <dependency> --> | ||
<!-- <groupId>junit</groupId> --> | ||
<!-- <artifactId>junit</artifactId> --> | ||
<!-- <version>3.8.1</version> --> | ||
<!-- <scope>test</scope> --> | ||
<!-- </dependency> --> | ||
<!-- <dependency> --> | ||
<!-- <groupId>org.apache.commons</groupId> --> | ||
<!-- <artifactId>commons-math3</artifactId> --> | ||
<!-- <version>3.5</version> --> | ||
<!-- </dependency> --> | ||
<!-- <dependency> --> | ||
<!-- <groupId>fr.ign.cogit</groupId> --> | ||
<!-- <artifactId>geoxygene-cartagen</artifactId> --> | ||
<!-- <version>1.9-SNAPSHOT</version> --> | ||
<!-- </dependency> --> | ||
<dependency> | ||
<groupId>fr.ign.cogit</groupId> | ||
<artifactId>geoxygene-api</artifactId> | ||
<version>${geoxcore.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>fr.ign.cogit</groupId> | ||
<artifactId>geoxygene-feature</artifactId> | ||
<version>${geoxcore.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>fr.ign.cogit</groupId> | ||
<artifactId>geoxygene-util</artifactId> | ||
<version>${geoxcore.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>fr.ign.cogit</groupId> | ||
<artifactId>geoxygene-sig3d</artifactId> | ||
<version>${geoxcore.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<version>3.6</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<repositories> | ||
<repository> | ||
<releases> | ||
<enabled>false</enabled> | ||
</releases> | ||
<snapshots> | ||
<enabled>true</enabled> | ||
</snapshots> | ||
<id>cogit-snapshots</id> | ||
<name>Cogit Snapshots Repository</name> | ||
<url>https://forge-cogit.ign.fr/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<!-- <repository> --> | ||
<!-- <id>apache.snapshots</id> --> | ||
<!-- <name>Apache commons</name> --> | ||
<!-- <url>http://repository.apache.org/snapshots/</url> --> | ||
<!-- </repository> --> | ||
</repositories> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Properties; | ||
|
||
import org.apache.commons.lang.math.NumberUtils; | ||
|
||
import fr.ign.cogit.geoxygene.api.feature.IFeature; | ||
import fr.ign.cogit.geoxygene.api.feature.IPopulation; | ||
import fr.ign.cogit.geoxygene.feature.DefaultFeature; | ||
import fr.ign.cogit.geoxygene.sig3d.io.vector.PostgisManager; | ||
import fr.ign.cogit.geoxygene.util.attribute.AttributeManager; | ||
import fr.ign.cogit.geoxygene.util.conversion.ShapefileReader; | ||
|
||
public class Simplushp2pgsql { | ||
public static boolean tableExists = false; | ||
|
||
// nested class to avoid some verbosity | ||
// really a glorified struct.. | ||
private static class Credential { | ||
private String host; | ||
private String db; | ||
private String port; | ||
private String user; | ||
private String password; | ||
private String table; | ||
private String shapesDir; | ||
|
||
private Credential(String host, String db, String port, String user, String password, String table, String dirPath) { | ||
this.host = host; | ||
this.db = db; | ||
this.port = port; | ||
this.user = user; | ||
this.password = password; | ||
this.table = table; | ||
this.shapesDir = dirPath; | ||
} | ||
|
||
public static Credential makeCredential(String dbCreds) { | ||
Properties creds = new Properties(); | ||
Reader reader; | ||
try { | ||
reader = new FileReader(dbCreds); | ||
creds.load(reader); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
return new Credential(creds.getProperty("host"), creds.getProperty("db"), creds.getProperty("port"), creds.getProperty("user"), | ||
creds.getProperty("password"), creds.getProperty("table"), creds.getProperty("shapesDir")); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param dirPath directory containing all the results subdirectories for a simulation | ||
* @return a list of Path corresponding to all subdirectories of dirPath | ||
*/ | ||
public static List<Path> getSubdirs(String dirPath) { | ||
Path dir = Paths.get(dirPath); | ||
DirectoryStream<Path> stream = null; | ||
try { | ||
stream = Files.newDirectoryStream(dir, p -> Files.isDirectory(p)); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
List<Path> dirs = new ArrayList<>(); | ||
for (Path p : stream) { | ||
dirs.add(p); | ||
} | ||
return dirs; | ||
} | ||
|
||
/** | ||
* check if the run identifier is correctly formated as YYMMDD | ||
* @param run | ||
* @return | ||
*/ | ||
public static boolean formatIsOk(String run) { | ||
if (run.length() != 6 && !NumberUtils.isDigits(run)) | ||
return false; | ||
SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd"); | ||
sdf.setLenient(false); | ||
try { | ||
sdf.parse(run); | ||
} catch (ParseException e) { | ||
System.out.println("bad format"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Write the shapefiles in dirPath with the 'run' identifier in the database referenced by creds | ||
* @param dirPath | ||
* @param run | ||
* @param creds | ||
*/ | ||
public static void writeShapetoDB(Path dirPath, String run, Credential creds) { | ||
String imu = "" + dirPath.getFileName(); | ||
List<String> dirs = new ArrayList<>(); | ||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dirPath, "*.shp")) { | ||
for (Path p : stream) { | ||
dirs.add(p.toString()); | ||
IPopulation<IFeature> shapes = ShapefileReader.read(p.toString()); | ||
for (IFeature f : shapes) { | ||
DefaultFeature df = (DefaultFeature) f; | ||
AttributeManager.addAttribute(df, "directory", imu, "String"); | ||
AttributeManager.addAttribute(df, "run", run, "String"); | ||
System.out.println(df.getGeom()); | ||
} | ||
System.out.println(p + " -> " + imu + " : " + shapes.size() + " features"); | ||
if (tableExists) { | ||
// PostgisManager.insertInGeometricTable(creds.host"localhost", "5432", "iudf2", "imrandb", "imrandb", "asd", shapes); | ||
PostgisManager.insertInGeometricTable(creds.host, creds.port, creds.db, creds.user, creds.password, creds.table, shapes); | ||
} else { | ||
// PostgisManager.saveGeometricTable("localhost", "5432", "iudf2", "imrandb", "imrandb", "asd", shapes, false); | ||
PostgisManager.saveGeometricTable(creds.host, creds.port, creds.db, creds.user, creds.password, creds.table, shapes, false); | ||
tableExists = true; | ||
} | ||
// System.out.println(shapes); | ||
} | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
if (args.length != 2) { | ||
System.out.println( | ||
"Proper Usage is:\n simplushp2pgsql conf run\nWith conf being the path to the credentials file and run like YYMMDD\nExample :\n simplushp2pgsql /home/operator/creds.conf 170518"); | ||
System.exit(0); | ||
} | ||
String dbCreds = "/home/imran/credsimplu.conf"; | ||
dbCreds = args[0]; | ||
// Credential conf = getDbSrcTarg(dbCreds); | ||
Credential conf = Credential.makeCredential(dbCreds); | ||
String run = "170518"; | ||
run = args[1]; | ||
if (formatIsOk(run)) { | ||
System.out.println("incorrect format for run : it should be as YYMMDD like 170518"); | ||
System.exit(0); | ||
} | ||
List<Path> ll = getSubdirs(conf.shapesDir); | ||
System.out.println(ll.size()); | ||
for (Path dir : ll) { | ||
writeShapetoDB(dir, run, conf); | ||
} | ||
} | ||
|
||
} |