Skip to content

Commit a6e1927

Browse files
committed
If field types in csv are present, use them to construct the schema
1 parent b7b349d commit a6e1927

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/main/groovy/geoscript/layer/io/CsvReader.groovy

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ class CsvReader implements Reader {
329329
}
330330
}
331331
// Try to guess the geometry Field
332-
if (!hasGeom && (c.toLowerCase().contains("geom") || c.toLowerCase().contains("shape"))) {
333-
fieldType = "Point"
332+
if (!hasGeom && (isGeometry(fieldType) || c.toLowerCase().contains("geom") || c.toLowerCase().contains("shape"))) {
333+
fieldType = fieldType.equalsIgnoreCase("String") ? "Point" : fieldType
334334
hasGeom = true
335335
}
336336
new Field(c, fieldType, proj)
@@ -345,6 +345,19 @@ class CsvReader implements Reader {
345345
return layer
346346
}
347347

348+
/**
349+
* Determine whether the field type is a geometry field type or nor
350+
* @param fieldType The field type
351+
* @return Whether the field type is a geometry field type
352+
*/
353+
private boolean isGeometry(String fieldType) {
354+
List geometryNames = [
355+
"point","linestring","polygon","linearring","geometry","geometrycollection",
356+
"circularstring","circularring","compoundring","compoundcurve"
357+
]
358+
geometryNames.any{geomName -> fieldType.toLowerCase().endsWith(geomName)}
359+
}
360+
348361
/**
349362
* Get the column name from a column name string that may or may not have type and projection information
350363
* @param col The column name string

src/test/groovy/geoscript/layer/io/CsvReaderTestCase.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,33 @@ ak,10501917,?,"Thursday, June 28, 2012 02:30:58 UTC",60.0233,-152.9946,2,2.9,?,"
429429
assertEquals("csv geom: Point(EPSG:4326), id: Integer, name: String", layer.schema.toString())
430430
}
431431

432+
@Test void readNoFeaturesWithLineStringType() {
433+
String csv = """"abc:LineString:EPSG:2927","id:int","name:String"
434+
"""
435+
CsvReader reader = new CsvReader()
436+
Layer layer = reader.read(csv)
437+
assertEquals(0, layer.count)
438+
assertEquals("csv abc: LineString(EPSG:2927), id: Integer, name: String", layer.schema.toString())
439+
}
440+
441+
@Test void readNoFeatureWithPolygonType() {
442+
String csv = "\"the_geom:MultiPolygon:EPSG:4326\",\"STATE_NAME:String\",\"STATE_FIPS:String\"," +
443+
"\"SUB_REGION:String\",\"STATE_ABBR:String\",\"LAND_KM:Double\",\"WATER_KM:Double\"," +
444+
"\"PERSONS:Double\",\"FAMILIES:Double\",\"HOUSHOLD:Double\",\"MALE:Double\"," +
445+
"\"FEMALE:Double\",\"WORKERS:Double\",\"DRVALONE:Double\",\"CARPOOL:Double\"," +
446+
"\"PUBTRANS:Double\",\"EMPLOYED:Double\",\"UNEMPLOY:Double\",\"SERVICE:Double\"," +
447+
"\"MANUAL:Double\",\"P_MALE:Double\",\"P_FEMALE:Double\",\"SAMP_POP:Double\""
448+
CsvReader reader = new CsvReader()
449+
Layer layer = reader.read(csv)
450+
assertEquals(0, layer.count)
451+
assertEquals("csv the_geom: MultiPolygon(EPSG:4326), STATE_NAME: String, STATE_FIPS: String, " +
452+
"SUB_REGION: String, STATE_ABBR: String, LAND_KM: Double, WATER_KM: Double, PERSONS: Double, " +
453+
"FAMILIES: Double, HOUSHOLD: Double, MALE: Double, FEMALE: Double, WORKERS: Double, " +
454+
"DRVALONE: Double, CARPOOL: Double, PUBTRANS: Double, EMPLOYED: Double, UNEMPLOY: Double, " +
455+
"SERVICE: Double, MANUAL: Double, P_MALE: Double, P_FEMALE: Double, SAMP_POP: Double",
456+
layer.schema.toString())
457+
}
458+
432459
@Test void readFromWktWithNoSpace() {
433460
String csv = """the_geom,ADMIN_NAME
434461
POINT(10.1999998092651 59.7000007629395),Buskerud

0 commit comments

Comments
 (0)