-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from fwiesweg/master
Allow joining external data using tag-transform
- Loading branch information
Showing
18 changed files
with
706 additions
and
27 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
8 changes: 8 additions & 0 deletions
8
osmosis-tagtransform/src/main/java/org/openstreetmap/osmosis/tagtransform/DataSource.java
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,8 @@ | ||
// This software is released into the Public Domain. See copying.txt for details. | ||
package org.openstreetmap.osmosis.tagtransform; | ||
|
||
public interface DataSource { | ||
|
||
String[] transform(String[] matches); | ||
|
||
} |
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
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
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
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
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
75 changes: 75 additions & 0 deletions
75
...tagtransform/src/main/java/org/openstreetmap/osmosis/tagtransform/impl/DataSourceCSV.java
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,75 @@ | ||
// This software is released into the Public Domain. See copying.txt for details. | ||
|
||
package org.openstreetmap.osmosis.tagtransform.impl; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.apache.commons.csv.CSVFormat; | ||
import org.apache.commons.csv.CSVParser; | ||
import org.apache.commons.csv.CSVRecord; | ||
import org.apache.commons.csv.QuoteMode; | ||
import org.w3c.dom.NamedNodeMap; | ||
|
||
import org.openstreetmap.osmosis.tagtransform.DataSource; | ||
|
||
/** | ||
* | ||
* @author fwiesweg | ||
*/ | ||
public class DataSourceCSV implements DataSource { | ||
|
||
private Map<String, String[]> data = Collections.emptyMap(); | ||
private String[] fallback; | ||
|
||
public DataSourceCSV(File parentDirectory, NamedNodeMap attributes) { | ||
String file = attributes.getNamedItem("file").getTextContent(); | ||
String csvFormat = attributes.getNamedItem("csvFormat").getTextContent(); | ||
|
||
|
||
List<Integer> lookupIndices = Stream.of(attributes.getNamedItem("lookup").getTextContent().split(",")) | ||
.map(idx -> Integer.parseInt(idx)) | ||
.collect(Collectors.toList()); | ||
List<Integer> returnIndices = Stream.of(attributes.getNamedItem("return").getTextContent().split(",")) | ||
.map(idx -> Integer.parseInt(idx)) | ||
.collect(Collectors.toList()); | ||
|
||
try(InputStreamReader fis = new InputStreamReader(new FileInputStream(new File(parentDirectory, file)))) { | ||
CSVRecord fallbackRecord = CSVParser.parse(attributes.getNamedItem("fallback").getTextContent(), CSVFormat.newFormat(',') | ||
.withDelimiter(',') | ||
.withQuote('"') | ||
.withQuoteMode(QuoteMode.MINIMAL)) | ||
.iterator().next(); | ||
this.fallback = new String[fallbackRecord.size()]; | ||
for(int i = 0; i < fallback.length; i++) { | ||
this.fallback[i] = fallbackRecord.get(i); | ||
} | ||
|
||
CSVParser parser = CSVParser.parse(fis, CSVFormat.valueOf(csvFormat)); | ||
this.data = parser.getRecords().stream().collect(Collectors.toMap( | ||
r -> { | ||
return lookupIndices.stream().map(i -> r.get(i)).collect(Collectors.joining("\0")); | ||
}, | ||
r -> { | ||
return returnIndices.stream().map(i -> r.get(i)).toArray(i -> new String[i]); | ||
})); | ||
|
||
|
||
} catch (IOException ex) { | ||
Logger.getLogger(DataSourceCSV.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public String[] transform(String[] matches) { | ||
return data.getOrDefault(String.join("\0", matches), fallback); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...s-tagtransform/src/main/java/org/openstreetmap/osmosis/tagtransform/impl/DataSources.java
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,26 @@ | ||
// This software is released into the Public Domain. See copying.txt for details. | ||
package org.openstreetmap.osmosis.tagtransform.impl; | ||
|
||
import java.io.File; | ||
import java.util.function.BiFunction; | ||
import org.w3c.dom.NamedNodeMap; | ||
|
||
import org.openstreetmap.osmosis.tagtransform.DataSource; | ||
|
||
/** | ||
* | ||
* @author fwiesweg | ||
*/ | ||
public enum DataSources { | ||
CSV(DataSourceCSV::new); | ||
|
||
private final BiFunction<File, NamedNodeMap, DataSource> factory; | ||
|
||
private DataSources(BiFunction<File, NamedNodeMap, DataSource> factory) { | ||
this.factory = factory; | ||
} | ||
|
||
public DataSource create(File parentDir, NamedNodeMap attributes) { | ||
return this.factory.apply(parentDir, attributes); | ||
} | ||
} |
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
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
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
Oops, something went wrong.