/** * Creates a new spatial file. * * @param f The raw file. * @param format The spatial format of the file. */ protected SpatialFile newSpatialFile(File f, DataFormat format) { SpatialFile sf = new SpatialFile(f); sf.setFormat(format); return sf; }
/** * Creates a new spatial file. * * @param f The raw file. * @param format The spatial format of the file. */ protected SpatialFile newSpatialFile(File f, DataFormat format) { SpatialFile sf = new SpatialFile(f); sf.setFormat(format); return sf; }
public static FileData createFromFile(File file) throws IOException { if (file.isDirectory()) { return new Directory(file); } if (new VFSWorker().canHandle(file)) { return new Archive(file); } return new SpatialFile(file); } public File getFile() {
public static FileData createFromFile(File file) throws IOException { if (file.isDirectory()) { return new Directory(file); } if (new VFSWorker().canHandle(file)) { return new Archive(file); } return new SpatialFile(file); }
@Test public void testCreateContextSingleFile() throws Exception { File dir = unpack("shape/archsites_epsg_prj.zip"); SpatialFile file = new SpatialFile(new File(dir, "archsites.shp")); file.prepare(); ImportContext context = importer.createContext(file); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(file, task.getData()); }
@Test public void testArchiveOnIndirectImport() throws Exception { File dir = unpack("shape/archsites_epsg_prj.zip"); assertTrue(dir.exists()); DataStoreInfo ds = createH2DataStore(null, "foo"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "archsites.shp")), ds); context.setArchive(true); importer.run(context); // under windows the shp in the original folder remains locked, but we could // not figure out why (a test in ShapefileDataStoreTest shows we can read a shapefile // and then delete the shp file without issues) if (!SystemUtils.IS_OS_WINDOWS) { assertFalse(dir.exists()); } dir = unpack("shape/bugsites_esri_prj.tar.gz"); assertTrue(dir.exists()); context = importer.createContext(new SpatialFile(new File(dir, "bugsites.shp")), ds); context.setArchive(false); importer.run(context); assertTrue(dir.exists()); }
@Test public void testImportNameClash() throws Exception { File dir = unpack("shape/archsites_epsg_prj.zip"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "archsites.shp"))); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("archsites")); runChecks("archsites"); context = importer.createContext(new SpatialFile(new File(dir, "archsites.shp"))); importer.run(context); assertEquals("archsites0", context.getTasks().get(0).getLayer().getName()); runChecks("archsites0"); }
@Test public void testJaggedGeoJSON() throws Exception { File json = file("geojson/jagged.json"); ImportContext ctx = importer.createContext(new SpatialFile(json)); assertEquals(1, ctx.getTasks().size()); SimpleFeatureType info = (SimpleFeatureType) ctx.getTasks().get(0).getMetadata().get(FeatureType.class); assertEquals(4, info.getAttributeCount()); int cnt = 0; for (int i = 0; i < info.getAttributeCount(); i++) { if (info.getDescriptor(i).getLocalName().equals("geometry")) { cnt++; } } assertEquals(1, cnt); }
@Test public void testGeoJSONImport() throws Exception { DataStoreInfo h2 = createH2DataStore(getCatalog().getDefaultWorkspace().getName(), "jsontest"); File dir = unpack("geojson/point.json.zip"); ImportContext imp = importer.createContext(new SpatialFile(new File(dir, "point.json")), h2); assertEquals(1, imp.getTasks().size()); assertEquals(ImportTask.State.READY, imp.task(0).getState()); importer.run(imp); assertEquals(ImportContext.State.COMPLETE, imp.getState()); checkNoErrors(imp); runChecks("point"); }
@Test public void testImportShapefile() throws Exception { File dir = unpack("shape/archsites_epsg_prj.zip"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "archsites.shp"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.READY, task.getState()); assertEquals("archsites", task.getLayer().getResource().getName()); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("archsites")); assertEquals(ImportTask.State.COMPLETE, task.getState()); runChecks("archsites"); }
@Test public void testImportGeoTIFF() throws Exception { File dir = unpack("geotiff/EmissiveCampania.tif.bz2"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "EmissiveCampania.tif"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.READY, task.getState()); assertEquals("EmissiveCampania", task.getLayer().getResource().getName()); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("EmissiveCampania")); assertEquals(ImportTask.State.COMPLETE, task.getState()); runChecks("EmissiveCampania"); }
importer.createContext(new SpatialFile(new File(dir, "archsites.shp"))); assertEquals(1, context.getTasks().size());
@Test public void testImportShapefileFromDataDir() throws Exception { File dataDir = getCatalog().getResourceLoader().getBaseDirectory(); File dir = unpack("shape/archsites_epsg_prj.zip", dataDir); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "archsites.shp"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.READY, task.getState()); assertEquals("archsites", task.getLayer().getResource().getName()); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("archsites")); assertEquals(ImportTask.State.COMPLETE, task.getState()); assertEquals( "file:archsites.shp", task.getLayer().getResource().getStore().getConnectionParameters().get("url")); runChecks("archsites"); }
String wsName = getCatalog().getDefaultWorkspace().getName(); DataStoreInfo h2DataStore = createH2DataStore(wsName, "kmltest"); SpatialFile importData = new SpatialFile(new File(dir, "sample.kml")); ImportContext context = importer.createContext(importData, h2DataStore); assertEquals(1, context.getTasks().size());
@Test public void testImportGeoTIFFFromDataDir() throws Exception { File dataDir = getCatalog().getResourceLoader().getBaseDirectory(); File dir = unpack("geotiff/EmissiveCampania.tif.bz2", dataDir); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "EmissiveCampania.tif"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.READY, task.getState()); assertEquals("EmissiveCampania", task.getLayer().getResource().getName()); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("EmissiveCampania")); assertEquals(ImportTask.State.COMPLETE, task.getState()); assertEquals( "file:EmissiveCampania.tif", ((CoverageStoreInfo) task.getLayer().getResource().getStore()).getURL()); runChecks("EmissiveCampania"); }
@Test public void testImportCSV() throws Exception { File dir = unpack("csv/locations.zip"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "locations.csv"))); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.NO_CRS, task.getState()); LayerInfo layer = task.getLayer(); ResourceInfo resource = layer.getResource(); resource.setSRS("EPSG:4326"); assertTrue("Item not ready", importer.prep(task)); assertEquals(ImportTask.State.READY, task.getState()); context.updated(); assertEquals(ImportContext.State.PENDING, context.getState()); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo fti = (FeatureTypeInfo) resource; SimpleFeatureType featureType = (SimpleFeatureType) fti.getFeatureType(); GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor(); assertNull("Expecting no geometry", geometryDescriptor); assertEquals(4, featureType.getAttributeCount()); }
@Test public void testDateFormatTransform() throws Exception { Catalog cat = getCatalog(); File dir = unpack("shape/ivan.zip"); SpatialFile file = new SpatialFile(new File(dir, "ivan.shp")); file.prepare(); ImportContext context = importer.createContext(file, store); assertEquals(1, context.getTasks().size()); context.setTargetStore(store); ImportTask task = context.getTasks().get(0); task.getTransform().add(new DateFormatTransform("timestamp", "yyyy-MM-dd HH:mm:ss.S")); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "ivan"); assertNotNull(ft); SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType(); assertTrue( Date.class.isAssignableFrom( schema.getDescriptor("timestamp").getType().getBinding())); FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features(); try { assertTrue(it.hasNext()); while (it.hasNext()) { SimpleFeature f = (SimpleFeature) it.next(); assertTrue(f.getAttribute("timestamp") instanceof Date); } } finally { it.close(); } }
@Test public void testImportSpaceInNames() throws Exception { File dir = unpack("shape/spaceInNames.zip"); DataStoreInfo ds = createH2DataStore(null, "spaceInNamesContainer"); ImportContext context = importer.createContext(new SpatialFile(new File(dir, "spaceInNames.shp")), ds); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.NO_CRS, task.getState()); task.getLayer().getResource().setSRS("EPSG:26713"); importer.changed(task); assertEquals(ImportTask.State.READY, task.getState()); assertEquals("spaceInNames", task.getLayer().getResource().getName()); importer.run(context); Catalog cat = getCatalog(); assertNotNull(cat.getLayerByName("spaceInNames")); assertEquals(ImportTask.State.COMPLETE, task.getState()); SimpleFeatureSource fs = (SimpleFeatureSource) cat.getFeatureTypeByName("spaceInNames").getFeatureSource(null, null); SimpleFeature sf = DataUtilities.first(fs.getFeatures()); assertNotNull(sf.getAttribute("WIND_SPEED")); assertNotNull(sf.getAttribute("WIND_DIREC")); }
@Test public void testNumberFormatTransform() throws Exception { Catalog cat = getCatalog(); File dir = unpack("shape/restricted.zip"); SpatialFile file = new SpatialFile(new File(dir, "restricted.shp")); file.prepare(); ImportContext context = importer.createContext(file, store); assertEquals(1, context.getTasks().size()); context.setTargetStore(store); ImportTask task = context.getTasks().get(0); task.getTransform().add(new NumberFormatTransform("cat", Integer.class)); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo ft = cat.getFeatureTypeByDataStore(store, "restricted"); assertNotNull(ft); SimpleFeatureType schema = (SimpleFeatureType) ft.getFeatureType(); assertEquals(Integer.class, schema.getDescriptor("cat").getType().getBinding()); FeatureIterator it = ft.getFeatureSource(null, null).getFeatures().features(); try { assertTrue(it.hasNext()); while (it.hasNext()) { SimpleFeature f = (SimpleFeature) it.next(); assertTrue(f.getAttribute("cat") instanceof Integer); } } finally { it.close(); } }
private void checkGMLPoiImport(File gmlFile, DataStoreInfo store) throws IOException, CQLException { SpatialFile importData = new SpatialFile(gmlFile); ImportContext context = importer.createContext(importData, store); assertEquals(1, context.getTasks().size()); ImportTask task = context.getTasks().get(0); assertEquals(ImportTask.State.READY, task.getState()); assertEquals(ImportContext.State.PENDING, context.getState()); importer.run(context); assertEquals(ImportContext.State.COMPLETE, context.getState()); FeatureTypeInfo fti = getCatalog().getResourceByName("poi", FeatureTypeInfo.class); assertNotNull(fti); SimpleFeatureType featureType = (SimpleFeatureType) fti.getFeatureType(); GeometryDescriptor geometryDescriptor = featureType.getGeometryDescriptor(); assertEquals( "Expecting a point geometry", Point.class, geometryDescriptor.getType().getBinding()); assertEquals(4, featureType.getAttributeCount()); // read the features, check they are in the right order SimpleFeatureSource fs = (SimpleFeatureSource) fti.getFeatureSource(null, null); SimpleFeatureCollection fc = fs.getFeatures(CQL.toFilter("NAME = 'museam'")); assertEquals(1, fc.size()); SimpleFeature sf = DataUtilities.first(fc); Point p = (Point) sf.getDefaultGeometry(); assertEquals(-74.0104611, p.getX(), 1e-6); assertEquals(40.70758763, p.getY(), 1e-6); }