CSV Plugin¶

The gt-csv module is a plugin which provides a CSVDataStore. It is pluggable into geoserver’s importer, allowing read/write capabilities for .csv files. The plugin supports three strategies: AttributesOnly, LatLon, and SpecifiedWKT. Essentially, each strategy will read and store data in a slightly different way.

The CSVWriteStrategyTest shows how the strategy code functions fairly well. A more detailed explanation can be found in the ContentDataStore tutorial on the strategy page.

CSVWriteStrategyTest.java

  • CSVAttributesOnlyStrategy

    This strategy will directly read the .csv file and use its headers as the attributes for each feature.

        @Test
        public void Attributes() throws Exception {
            CSVFileState fileState = new CSVFileState("CITY, NUMBER, YEAR", "TEST");
            CSVStrategy strategy = new CSVAttributesOnlyStrategy(fileState);
    
            SimpleFeatureType featureType = strategy.buildFeatureType();
            assertEquals("TEST", featureType.getName().getLocalPart());
            assertEquals(3, featureType.getAttributeCount());
    
            SimpleFeature feature =
                    SimpleFeatureBuilder.build(
                            featureType, new Object[] {"Trento", 140, 2002}, "TEST-fid1");
            String[] csvRecord = new String[] {"Trento", "140", "2002"};
            SimpleFeature parsed = strategy.decode("fid1", csvRecord);
            assertEquals(feature, parsed);
    
            String[] record = strategy.encode(feature);
            assertEquals(csvRecord.length, record.length);
            if (csvRecord.length == record.length) {
                for (int i = 0; i < csvRecord.length; i++) {
                    assertEquals(csvRecord[i], record[i]);
                }
            }
        }
    
  • CSVLatLonStrategy

    The LatLonStrategy will attempt to parse the .csv file for LAT and LON columns. If specified, it will use the String parameters passed to it as the names of the columns. Otherwise, it will attempt to find the columns using some standard spellings and abbrieviations for latitude and longitude. Internally, the data will be represented as a Point geometry.

        @Test
        public void LatLon() throws Exception {
            CSVFileState fileState = new CSVFileState("LAT, LON, CITY, NUMBER, YEAR", "TEST");
            CSVStrategy strategy = new CSVLatLonStrategy(fileState);
    
            SimpleFeatureType featureType = strategy.buildFeatureType();
            assertEquals("TEST", featureType.getName().getLocalPart());
            // 4 because LAT/LON should be stored internally as POINT
            assertEquals(4, featureType.getAttributeCount());
    
            GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
            Point trento = gf.createPoint(new Coordinate(46.066667, 11.116667));
            SimpleFeature feature =
                    SimpleFeatureBuilder.build(
                            featureType, new Object[] {trento, "Trento", 140, 2002}, "TEST-fid1");
            String[] csvRecord = new String[] {"46.066667", "11.116667", "Trento", "140", "2002"};
            SimpleFeature parsed = strategy.decode("fid1", csvRecord);
            assertEquals(feature, parsed);
    
            String[] record = strategy.encode(feature);
            assertEquals(csvRecord.length, record.length);
            if (csvRecord.length == record.length) {
                for (int i = 0; i < csvRecord.length; i++) {
                    assertEquals(csvRecord[i], record[i]);
                }
            }
        }
    
  • CSVSpecifiedWKTStrategy

    Finally, the SpecifiedWKTStrategy requires a WKT which it will parse for. It is a bit more abstract than the LatLonStrategy, and will store the specified column as a Geometry attribute (it may be a Point, Line, Polygon, etc.).

        @Test
        public void SpecifiedLatLon() throws Exception {
            CSVFileState fileState = new CSVFileState("TAL, NOL, CITY, NUMBER, YEAR", "TEST");
            CSVStrategy strategy = new CSVLatLonStrategy(fileState, "TAL", "NOL");
    
            SimpleFeatureType featureType = strategy.buildFeatureType();
            assertEquals("TEST", featureType.getName().getLocalPart());
            // 4 because LAT/LON should be stored internally as POINT
            assertEquals(4, featureType.getAttributeCount());
    
            GeometryFactory gf = JTSFactoryFinder.getGeometryFactory();
            Point trento = gf.createPoint(new Coordinate(46.066667, 11.116667));
            SimpleFeature feature =
                    SimpleFeatureBuilder.build(
                            featureType, new Object[] {trento, "Trento", 140, 2002}, "TEST-fid1");
            String[] csvRecord = new String[] {"46.066667", "11.116667", "Trento", "140", "2002"};
            SimpleFeature parsed = strategy.decode("fid1", csvRecord);
            assertEquals(feature, parsed);
    
            String[] record = strategy.encode(feature);
            assertEquals(csvRecord.length, record.length);
            if (csvRecord.length == record.length) {
                for (int i = 0; i < csvRecord.length; i++) {
                    assertEquals(csvRecord[i], record[i]);
                }
            }
        }