Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
giscience
big-data
ohsome
helpers
oshdb database driver
Commits
0811e12d
Commit
0811e12d
authored
Jan 28, 2020
by
Rafael Troilo
Browse files
oshdb database driver
parents
Changes
2
Hide whitespace changes
Inline
Side-by-side
pom.xml
0 → 100644
View file @
0811e12d
<?xml version="1.0" encoding="UTF-8"?>
<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>
org.heigit.ohsome.oshdb.helpers
</groupId>
<artifactId>
OSHDBDriver
</artifactId>
<version>
1.0-SNAPSHOT
</version>
<properties>
<maven.compiler.target>
1.8
</maven.compiler.target>
<maven.compiler.source>
1.8
</maven.compiler.source>
</properties>
<dependencies>
<!-- declare the dependency to be set as optional -->
<dependency>
<groupId>
org.heigit.bigspatialdata
</groupId>
<artifactId>
oshdb-api
</artifactId>
<version>
0.5.5
</version>
<scope>
compile
</scope>
<optional>
true
</optional>
</dependency>
</dependencies>
</project>
\ No newline at end of file
src/main/java/org/heigit/ohsome/oshdb/helpers/OSHDBDriver.java
0 → 100644
View file @
0811e12d
package
org.heigit.ohsome.oshdb.helpers
;
import
org.heigit.bigspatialdata.oshdb.api.db.OSHDBDatabase
;
import
org.heigit.bigspatialdata.oshdb.api.db.OSHDBH2
;
import
org.heigit.bigspatialdata.oshdb.api.db.OSHDBIgnite
;
import
org.heigit.bigspatialdata.oshdb.api.db.OSHDBJdbc
;
import
org.heigit.bigspatialdata.oshdb.api.mapreducer.MapReducer
;
import
org.heigit.bigspatialdata.oshdb.api.mapreducer.OSMContributionView
;
import
org.heigit.bigspatialdata.oshdb.api.mapreducer.OSMEntitySnapshotView
;
import
org.heigit.bigspatialdata.oshdb.api.object.OSMContribution
;
import
org.heigit.bigspatialdata.oshdb.api.object.OSMEntitySnapshot
;
import
org.heigit.bigspatialdata.oshdb.util.exceptions.OSHDBKeytablesNotFoundException
;
import
org.heigit.bigspatialdata.oshdb.util.tagtranslator.TagTranslator
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.util.Map
;
import
java.util.Properties
;
import
java.util.function.Consumer
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
/**
* A basic OSHDBDriver class for connecting to h2 or ignite oshdb instances.
*
* <pre>{@code
* OSHDBDriver.connect(props, (oshdb) -> {
* oshdb.getSnapshotView()
* .areaOfInterest((Geometry & Polygonal) areaOfInterest)
* .timestamps(tstamps)
* .osmTag(key)
* ...
* });
* }</pre>
*/
public
class
OSHDBDriver
{
/*
props example:
oshdb="ignite:Path_To_Config" \ "h2:Path_To_Database"
prefix="global"
keytable="jdbc:postgresql://IP_OR_URL/keytables\${prefix}?user=ohsome&password=secret"
#multithreading default false, only used by h2 connections
multithreading=true
*/
public
static
void
connect
(
Map
<
String
,
String
>
props
,
Consumer
<
OSHDBConnection
>
connect
)
throws
Exception
{
String
oshdb
=
getInterpolated
(
props
,
"oshdb"
);
if
(
oshdb
==
null
){
throw
new
IllegalArgumentException
(
"props need to have to specifiy oshdb!"
);
}
if
(
oshdb
.
toLowerCase
().
startsWith
(
"ignite:"
)){
String
cfg
=
oshdb
.
substring
(
7
);
String
prefix
=
getInterpolated
(
props
,
"prefix"
,
""
);
String
keyTableUrl
=
getInterpolated
(
props
,
"keytable"
);
if
(
keyTableUrl
==
null
){
throw
new
IllegalArgumentException
(
"ignite specified but missing keytable"
);
}
connectToIgnite
(
cfg
,
prefix
,
keyTableUrl
,
connect
);
}
else
if
(
oshdb
.
toLowerCase
().
startsWith
(
"h2:"
))
{
String
h2
=
oshdb
.
substring
(
3
);
String
prefix
=
getInterpolated
(
props
,
"prefix"
,
""
);
boolean
multithreading
=
"true"
.
equalsIgnoreCase
(
getInterpolated
(
props
,
"multithreading"
));
connectToH2
(
h2
,
prefix
,
multithreading
,
connect
);
}
else
{
throw
new
IllegalArgumentException
(
"unknown oshdb value! "
+
oshdb
);
}
}
public
static
void
connectToH2
(
String
url
,
String
prefix
,
Consumer
<
OSHDBConnection
>
connect
)
throws
Exception
{
connectToH2
(
url
,
prefix
,
true
,
connect
);
}
public
static
void
connectToH2
(
String
h2
,
String
prefix
,
boolean
multithreading
,
Consumer
<
OSHDBConnection
>
connect
)
throws
Exception
{
try
(
final
OSHDBH2
oshdb
=
new
OSHDBH2
(
h2
);
final
OSHDBJdbc
keyTables
=
new
OSHDBJdbc
(
oshdb
.
getConnection
()))
{
oshdb
.
prefix
(
prefix
);
oshdb
.
multithreading
(
multithreading
);
final
OSHDBConnection
connection
=
new
OSHDBConnection
(
oshdb
,
keyTables
);
connect
.
accept
(
connection
);
}
}
public
static
void
connectToIgnite
(
String
cfg
,
String
prefix
,
String
keytableUrl
,
Consumer
<
OSHDBConnection
>
connect
)
throws
Exception
{
try
(
final
Connection
ktConnection
=
DriverManager
.
getConnection
(
keytableUrl
);
final
OSHDBJdbc
keytables
=
new
OSHDBJdbc
(
ktConnection
);
final
OSHDBIgnite
oshdb
=
new
OSHDBIgnite
(
cfg
))
{
oshdb
.
prefix
(
prefix
);
final
OSHDBConnection
connection
=
new
OSHDBConnection
(
oshdb
,
keytables
);
connect
.
accept
(
connection
);
}
}
public
static
class
OSHDBConnection
{
private
final
OSHDBDatabase
oshdb
;
private
final
OSHDBJdbc
keytables
;
private
final
TagTranslator
tagTranslator
;
public
OSHDBConnection
(
OSHDBDatabase
oshdb
,
OSHDBJdbc
keytables
)
throws
OSHDBKeytablesNotFoundException
{
this
.
oshdb
=
oshdb
;
this
.
keytables
=
keytables
;
this
.
tagTranslator
=
new
TagTranslator
(
keytables
.
getConnection
());
}
public
MapReducer
<
OSMContribution
>
getContributionView
()
{
return
OSMContributionView
.
on
(
oshdb
).
keytables
(
keytables
);
}
public
MapReducer
<
OSMEntitySnapshot
>
getSnapshotView
()
{
return
OSMEntitySnapshotView
.
on
(
oshdb
).
keytables
(
keytables
);
}
public
OSHDBDatabase
getOSHDB
()
{
return
oshdb
;
}
public
OSHDBJdbc
getKeytables
()
{
return
keytables
;
}
public
TagTranslator
getTagTranslator
()
{
return
tagTranslator
;
}
}
private
static
final
Pattern
substitute
=
Pattern
.
compile
(
"\\$\\{(\\w+)\\}"
);
private
static
String
getInterpolated
(
Map
<
String
,
String
>
props
,
String
key
){
return
getInterpolated
(
props
,
key
,
null
);
}
private
static
String
getInterpolated
(
Map
<
String
,
String
>
props
,
String
key
,
String
defaultValue
){
String
value
=
props
.
get
(
key
);
if
(
value
==
null
){
return
defaultValue
;
}
Matcher
matcher
=
substitute
.
matcher
(
value
);
StringBuffer
sb
=
new
StringBuffer
();
while
(
matcher
.
find
()){
String
sub
=
matcher
.
group
(
1
);
matcher
.
appendReplacement
(
sb
,
props
.
getOrDefault
(
sub
,
"\\${"
+
sub
+
"}"
));
}
matcher
.
appendTail
(
sb
);
return
sb
.
toString
();
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment