Skip to content

Commit

Permalink
CSV DataStore better support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuarte47 committed Oct 16, 2017
1 parent 3c1b593 commit 4208b8a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
Expand Up @@ -237,6 +237,15 @@ public <T> T convert(Object source, Class<T> target) throws Exception {
}
catch( Exception ignore){
}
for (String formatPattern : new String[]{ "yyyy-MM-dd HH:mm:ss", "yyyy-MM-ddTHH:mm:ss", "yyyy-MM-dd" }) {
try {
SimpleDateFormat format3 = new SimpleDateFormat(formatPattern);
Date parsed = format3.parse(string);
return target.cast(parsed);
}
catch( Exception ignore){
}
}
return null;
}
}
Expand Down
Expand Up @@ -157,6 +157,25 @@ private FileDataStore createDataStoreFromFile(File file, URI namespace,
CSVFileState csvFileState = new CSVFileState(file, namespace);
Object strategyParam = STRATEGYP.lookUp(params);
CSVStrategy csvStrategy = null;

// Lookup geometry fields better.
if (strategyParam == null || params == null || params.size() == 0) {
org.geotools.feature.simple.SimpleFeatureTypeBuilder builder = CSVStrategy.createBuilder(csvFileState);

if (builder != null) {
org.opengis.feature.type.AttributeDescriptor lonDescr = builder.get("longitude");
org.opengis.feature.type.AttributeDescriptor latDescr = builder.get("latitude");
if (lonDescr == null || latDescr == null) {
lonDescr = builder.get("lon");
latDescr = builder.get("lat");
}
if (lonDescr != null && lonDescr.getType().getBinding().isAssignableFrom(Double.class) &&
latDescr != null && latDescr.getType().getBinding().isAssignableFrom(Double.class)) {
strategyParam = "guess";
}
}
}

if (strategyParam != null) {
String strategyString = strategyParam.toString();
if (strategyString.equalsIgnoreCase("guess")) {
Expand Down
Expand Up @@ -108,6 +108,12 @@ public static SimpleFeatureTypeBuilder createBuilder(CSVFileState csvFileState,
protected static Map<String, Class<?>> findMostSpecificTypesFromData(CsvReader csvReader,
String[] headers) throws IOException {
Map<String, Class<?>> result = new HashMap<String, Class<?>>();

List<java.time.format.DateTimeFormatter> formatterList = new java.util.ArrayList<java.time.format.DateTimeFormatter>();
formatterList.add(java.time.format.DateTimeFormatter.ISO_DATE_TIME);
formatterList.add(java.time.format.DateTimeFormatter.ISO_DATE);
int featureCount = 0;

// start off assuming Integers for everything
for (String header : headers) {
result.put(header, Integer.class);
Expand All @@ -123,6 +129,29 @@ protected static Map<String, Class<?>> findMostSpecificTypesFromData(CsvReader c
for (String value : values) {
String header = headers[i];
Class<?> type = result.get(header);

// Parse Boolean and Date/Time attribute types too.
if (value == null || value.length() == 0) {
i++;
continue;
}
if (type == Boolean.class && (!value.equalsIgnoreCase("true") && !value.equalsIgnoreCase("false"))) {
type = String.class;
}
if (type == Integer.class) {
if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) type = Boolean.class;
}
if (type == Integer.class || type == java.util.Date.class) {
for (int j = 0, jcount = formatterList.size(); j < jcount; j++) {
try {
formatterList.get(j).parse(value.replace(' ', 'T'));
type = java.util.Date.class;
break;
} catch (Exception ex) {
}
}
}

// For each value in the row, ensure we can still parse it as the
// defined type for this column; if not, make it more general
if (type == Integer.class) {
Expand All @@ -146,6 +175,10 @@ protected static Map<String, Class<?>> findMostSpecificTypesFromData(CsvReader c
result.put(header, type);
i++;
}

// Skip to parse very-big CSV files.
if (featureCount>=100) break;
featureCount++;
}
return result;
}
Expand Down

0 comments on commit 4208b8a

Please sign in to comment.