public void testImportIntoDatabaseWithEncoding() throws Exception { Catalog cat = getCatalog(); DataStoreInfo ds = createH2DataStore(cat.getDefaultWorkspace().getName(), "ming"); File dir = tmpDir(); unpack("shape/ming_time.zip", dir); ImportContext context = importer.createContext(new Directory(dir), ds); assertEquals(1, context.getTasks().size()); context.getTasks().get(0).getData().setCharsetEncoding("UTF-8"); importer.run(context); FeatureTypeInfo info = (FeatureTypeInfo) context.getTasks().get(0).getLayer().getResource(); FeatureSource<? extends FeatureType, ? extends Feature> fs = info.getFeatureSource(null, null); FeatureCollection<? extends FeatureType, ? extends Feature> features = fs.getFeatures(); FeatureIterator<? extends Feature> it = features.features(); assertTrue(it.hasNext()); SimpleFeature next = (SimpleFeature) it.next(); // let's test some attributes to see if they were digested properly String type_ch = (String) next.getAttribute("type_ch"); assertEquals("卫", type_ch); String name_ch = (String) next.getAttribute("name_ch"); assertEquals("杭州前卫", name_ch); it.close(); }
private WorkspaceInfo findWorkspace(String wsName) { Catalog cat = geoServer.getCatalog(); WorkspaceInfo ws; if ("default".equals(wsName)) { ws = cat.getDefaultWorkspace(); } else { ws = cat.getWorkspaceByName(wsName); } if (ws == null) { throw new RuntimeException("Unable to find workspace " + wsName); } return ws; }
public void testImportDatabase() throws Exception { File dir = unpack("h2/cookbook.zip"); Map params = new HashMap(); params.put(H2DataStoreFactory.DBTYPE.key, "h2"); params.put(H2DataStoreFactory.DATABASE.key, new File(dir, "cookbook").getAbsolutePath()); ImportContext context = importer.createContext(new Database(params)); assertEquals(3, context.getTasks().size()); assertEquals(ImportTask.State.READY, context.getTasks().get(0).getState()); assertEquals(ImportTask.State.READY, context.getTasks().get(1).getState()); assertEquals(ImportTask.State.READY, context.getTasks().get(2).getState()); Catalog cat = getCatalog(); assertNull(cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook")); assertNull(cat.getLayerByName("point")); assertNull(cat.getLayerByName("line")); assertNull(cat.getLayerByName("polygon")); importer.run(context); assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(0).getState()); assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(1).getState()); assertEquals(ImportTask.State.COMPLETE, context.getTasks().get(2).getState()); assertNotNull(cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook")); DataStoreInfo ds = cat.getDataStoreByName(cat.getDefaultWorkspace(), "cookbook"); assertNotNull(cat.getFeatureTypeByDataStore(ds, "point")); assertNotNull(cat.getFeatureTypeByDataStore(ds, "line")); assertNotNull(cat.getFeatureTypeByDataStore(ds, "polygon")); assertNotNull(cat.getLayerByName("point")); assertNotNull(cat.getLayerByName("line")); assertNotNull(cat.getLayerByName("polygon")); runChecks("point"); runChecks("line"); runChecks("polygon"); }
@Override protected void setUpInternal(SystemTestData data) throws Exception { // run all the tests against a store that can do native paging (h2) and one that // can't (property) Catalog cat = getCatalog(); DataStoreInfo ds = cat.getFactory().createDataStore(); ds.setName("foo"); ds.setWorkspace(cat.getDefaultWorkspace()); Map params = ds.getConnectionParameters(); params.put("dbtype", "h2"); params.put("database", getTestData().getDataDirectoryRoot().getAbsolutePath()); cat.add(ds); FeatureSource fs1 = getFeatureSource(SystemTestData.FIFTEEN); FeatureSource fs2 = getFeatureSource(SystemTestData.SEVEN); DataStore store = (DataStore) ds.getDataStore(null); SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder(); tb.init((SimpleFeatureType) fs1.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); tb.init((SimpleFeatureType) fs2.getSchema()); tb.add("num", Integer.class); tb.remove("boundedBy"); store.createSchema(tb.buildFeatureType()); CatalogBuilder cb = new CatalogBuilder(cat); cb.setStore(ds); FeatureStore fs = (FeatureStore) store.getFeatureSource("Fifteen"); addFeatures(fs, fs1.getFeatures()); FeatureTypeInfo ft = cb.buildFeatureType(fs); cat.add(ft); fs = (FeatureStore) store.getFeatureSource("Seven"); addFeatures(fs, fs2.getFeatures()); ft = cb.buildFeatureType(fs); cat.add(ft); }
@Override protected void setUpInternal() throws Exception { super.setUpInternal(); Catalog cat = getCatalog(); store = cat.getFactory().createDataStore(); store.setWorkspace(cat.getDefaultWorkspace()); store.setName("spearfish"); store.setType("H2"); Map params = new HashMap(); params.put("database", getTestData().getDataDirectoryRoot().getPath() + "/spearfish"); params.put("dbtype", "h2"); store.getConnectionParameters().putAll(params); store.setEnabled(true); cat.add(store); }
public void testImportIntoDatabase() throws Exception { Catalog cat = getCatalog(); DataStoreInfo ds = createH2DataStore(cat.getDefaultWorkspace().getName(), "spearfish"); File dir = tmpDir(); unpack("shape/archsites_epsg_prj.zip", dir); unpack("shape/bugsites_esri_prj.tar.gz", dir); ImportContext context = importer.createContext(new Directory(dir), ds); assertEquals(2, context.getTasks().size()); ImportTask task1 = context.getTasks().get(0); ImportTask task2 = context.getTasks().get(1); assertEquals(ImportTask.State.READY, task1.getState()); assertEquals(ImportTask.State.READY, task2.getState()); // assertEquals(ImportTask.State.READY, context.getTasks().get(1).getState()); // cannot ensure ordering of items HashSet resources = new HashSet(); resources.add(task1.getLayer().getResource().getName()); resources.add(task2.getLayer().getResource().getName()); assertTrue(resources.contains("bugsites")); assertTrue(resources.contains("archsites")); importer.run(context); assertEquals(ImportTask.State.COMPLETE, task1.getState()); assertEquals(ImportTask.State.COMPLETE, task2.getState()); assertNotNull(cat.getLayerByName("archsites")); assertNotNull(cat.getLayerByName("bugsites")); assertNotNull(cat.getFeatureTypeByDataStore(ds, "archsites")); assertNotNull(cat.getFeatureTypeByDataStore(ds, "bugsites")); runChecks("archsites"); runChecks("bugsites"); }
public MockCatalogBuilder commit() { if (!featureTypes.isEmpty() || !coverages.isEmpty()) { if (!featureTypes.isEmpty()) { DataStoreInfo ds = dataStores.peekLast(); expect(catalog.getResourcesByStore(ds, FeatureTypeInfo.class)) .andReturn(featureTypes) .anyTimes(); expect(catalog.getResourcesByStore(ds, ResourceInfo.class)) .andReturn((List) featureTypes) .anyTimes(); expect(catalog.getFeatureTypesByDataStore(ds)).andReturn(featureTypes).anyTimes(); } if (!coverages.isEmpty()) { CoverageStoreInfo cs = coverageStores.peekLast(); expect(catalog.getResourcesByStore(cs, CoverageInfo.class)).andReturn(coverages).anyTimes(); expect(catalog.getResourcesByStore(cs, ResourceInfo.class)) .andReturn((List) coverages) .anyTimes(); expect(catalog.getCoveragesByCoverageStore(cs)).andReturn(coverages).anyTimes(); } // clear out local lists but push up to be included when this workspace is complete featureTypesByNamespace.addAll(featureTypes); featureTypes = new LinkedList<FeatureTypeInfo>(); coveragesByNamespace.addAll(coverages); coverages = new LinkedList<CoverageInfo>(); } else if (!dataStores.isEmpty() || !coverageStores.isEmpty()) { WorkspaceInfo ws = workspaces.peekLast(); NamespaceInfo ns = namespaces.peekLast(); expect(catalog.getStoresByWorkspace(ws.getName(), DataStoreInfo.class)) .andReturn(dataStores) .anyTimes(); expect(catalog.getStoresByWorkspace(ws, DataStoreInfo.class)) .andReturn(dataStores) .anyTimes(); expect(catalog.getDataStoresByWorkspace(ws.getName())).andReturn(dataStores).anyTimes(); expect(catalog.getDataStoresByWorkspace(ws)).andReturn(dataStores).anyTimes(); expect(catalog.getStoresByWorkspace(ws.getName(), CoverageStoreInfo.class)) .andReturn(coverageStores) .anyTimes(); expect(catalog.getStoresByWorkspace(ws, CoverageStoreInfo.class)) .andReturn(coverageStores) .anyTimes(); expect(catalog.getCoverageStoresByWorkspace(ws.getName())) .andReturn(coverageStores) .anyTimes(); expect(catalog.getCoverageStoresByWorkspace(ws)).andReturn(coverageStores).anyTimes(); List<StoreInfo> l = new LinkedList<StoreInfo>(dataStores); l.addAll(coverageStores); expect(catalog.getStoresByWorkspace(ws.getName(), StoreInfo.class)).andReturn(l).anyTimes(); expect(catalog.getStoresByWorkspace(ws, StoreInfo.class)).andReturn(l).anyTimes(); // add all the resources for this workspace List<ResourceInfo> m = new LinkedList(featureTypesByNamespace); m.addAll(coveragesByNamespace); expect(catalog.getResourcesByNamespace(ns, ResourceInfo.class)).andReturn(m).anyTimes(); // expect(catalog.getResourcesByNamespace(ns.getPrefix(), // ResourceInfo.class)).andReturn(m).anyTimes(); expect(catalog.getResourcesByNamespace(ns, FeatureTypeInfo.class)) .andReturn(featureTypesByNamespace) .anyTimes(); // expect(catalog.getResourcesByNamespace(ns.getPrefix(), FeatureTypeInfo.class)) // .andReturn(featureTypesByNamespace).anyTimes(); expect(catalog.getResourcesByNamespace(ns, CoverageInfo.class)) .andReturn(coveragesByNamespace) .anyTimes(); // expect(catalog.getResourcesByNamespace(ns.getPrefix(), CoverageInfo.class)) // .andReturn(coveragesByNamespace).anyTimes(); dataStoresAll.addAll(dataStores); dataStores = new LinkedList(); coverageStoresAll.addAll(coverageStores); coverageStores = new LinkedList(); featureTypesAll.addAll(featureTypesByNamespace); featureTypesByNamespace = new LinkedList(); coveragesAll.addAll(coveragesByNamespace); coveragesByNamespace = new LinkedList(); } else if (!workspaces.isEmpty()) { // all the resources List<ResourceInfo> l = new LinkedList<ResourceInfo>(featureTypesAll); l.addAll(coveragesAll); expect(catalog.getResources(ResourceInfo.class)).andReturn(l).anyTimes(); expect(catalog.getResources(FeatureTypeInfo.class)).andReturn(featureTypesAll).anyTimes(); expect(catalog.getResources(CoverageInfo.class)).andReturn(coverages).anyTimes(); expect(catalog.getFeatureTypes()).andReturn(featureTypesAll).anyTimes(); expect(catalog.getCoverages()).andReturn(coveragesAll).anyTimes(); // add all the stores List<StoreInfo> m = new LinkedList<StoreInfo>(dataStoresAll); m.addAll(coverageStoresAll); expect(catalog.getStores(StoreInfo.class)).andReturn(m).anyTimes(); expect(catalog.getStores(DataStoreInfo.class)).andReturn(dataStoresAll).anyTimes(); expect(catalog.getStores(CoverageStoreInfo.class)).andReturn(coverageStoresAll).anyTimes(); // add all the styles expect(catalog.getStyles()).andReturn(styles).anyTimes(); // add all the layer groups expect(catalog.getLayerGroups()).andReturn(layerGroups).anyTimes(); // add all the workspaces/namespaces expect(catalog.getWorkspaces()).andReturn(workspaces).anyTimes(); expect(catalog.getNamespaces()).andReturn(namespaces).anyTimes(); // default workspace/namespace expect(catalog.getDefaultWorkspace()).andReturn(workspaces.peekFirst()).anyTimes(); expect(catalog.getDefaultNamespace()).andReturn(namespaces.peekFirst()).anyTimes(); replay(catalog); featureTypesAll = new LinkedList(); coveragesAll = new LinkedList(); dataStoresAll = new LinkedList(); coverageStoresAll = new LinkedList(); styles = new LinkedList(); workspaces = new LinkedList(); namespaces = new LinkedList(); } return this; }
@DescribeResult(name = "layerName", description = "Name of the new featuretype, with workspace") public String execute( @DescribeParameter(name = "features", min = 0, description = "Input feature collection") SimpleFeatureCollection features, @DescribeParameter(name = "coverage", min = 0, description = "Input raster") GridCoverage2D coverage, @DescribeParameter( name = "workspace", min = 0, description = "Target workspace (default is the system default)") String workspace, @DescribeParameter( name = "store", min = 0, description = "Target store (default is the workspace default)") String store, @DescribeParameter( name = "name", min = 0, description = "Name of the new featuretype/coverage (default is the name of the features in the collection)") String name, @DescribeParameter( name = "srs", min = 0, description = "Target coordinate reference system (default is based on source when possible)") CoordinateReferenceSystem srs, @DescribeParameter( name = "srsHandling", min = 0, description = "Desired SRS handling (default is FORCE_DECLARED, others are REPROJECT_TO_DECLARED or NONE)") ProjectionPolicy srsHandling, @DescribeParameter( name = "styleName", min = 0, description = "Name of the style to be associated with the layer (default is a standard geometry-specific style)") String styleName) throws ProcessException { // first off, decide what is the target store WorkspaceInfo ws; if (workspace != null) { ws = catalog.getWorkspaceByName(workspace); if (ws == null) { throw new ProcessException("Could not find workspace " + workspace); } } else { ws = catalog.getDefaultWorkspace(); if (ws == null) { throw new ProcessException("The catalog is empty, could not find a default workspace"); } } // create a builder to help build catalog objects CatalogBuilder cb = new CatalogBuilder(catalog); cb.setWorkspace(ws); // ok, find the target store StoreInfo storeInfo = null; boolean add = false; if (store != null) { if (features != null) { storeInfo = catalog.getDataStoreByName(ws.getName(), store); } else if (coverage != null) { storeInfo = catalog.getCoverageStoreByName(ws.getName(), store); } if (storeInfo == null) { throw new ProcessException("Could not find store " + store + " in workspace " + workspace); // TODO: support store creation } } else if (features != null) { storeInfo = catalog.getDefaultDataStore(ws); if (storeInfo == null) { throw new ProcessException("Could not find a default store in workspace " + ws.getName()); } } else if (coverage != null) { // create a new coverage store LOGGER.info( "Auto-configuring coverage store: " + (name != null ? name : coverage.getName().toString())); storeInfo = cb.buildCoverageStore((name != null ? name : coverage.getName().toString())); add = true; store = (name != null ? name : coverage.getName().toString()); if (storeInfo == null) { throw new ProcessException("Could not find a default store in workspace " + ws.getName()); } } // check the target style if any StyleInfo targetStyle = null; if (styleName != null) { targetStyle = catalog.getStyleByName(styleName); if (targetStyle == null) { throw new ProcessException("Could not find style " + styleName); } } if (features != null) { // check if the target layer and the target feature type are not // already there (this is a half-assed attempt as we don't have // an API telling us how the feature type name will be changed // by DataStore.createSchema(...), but better than fully importing // the data into the target store to find out we cannot create the layer...) String tentativeTargetName = null; if (name != null) { tentativeTargetName = ws.getName() + ":" + name; } else { tentativeTargetName = ws.getName() + ":" + features.getSchema().getTypeName(); } if (catalog.getLayer(tentativeTargetName) != null) { throw new ProcessException("Target layer " + tentativeTargetName + " already exists"); } // check the target crs String targetSRSCode = null; if (srs != null) { try { Integer code = CRS.lookupEpsgCode(srs, true); if (code == null) { throw new WPSException("Could not find a EPSG code for " + srs); } targetSRSCode = "EPSG:" + code; } catch (Exception e) { throw new ProcessException("Could not lookup the EPSG code for the provided srs", e); } } else { // check we can extract a code from the original data GeometryDescriptor gd = features.getSchema().getGeometryDescriptor(); if (gd == null) { // data is geometryless, we need a fake SRS targetSRSCode = "EPSG:4326"; srsHandling = ProjectionPolicy.FORCE_DECLARED; } else { CoordinateReferenceSystem nativeCrs = gd.getCoordinateReferenceSystem(); if (nativeCrs == null) { throw new ProcessException( "The original data has no native CRS, " + "you need to specify the srs parameter"); } else { try { Integer code = CRS.lookupEpsgCode(nativeCrs, true); if (code == null) { throw new ProcessException( "Could not find an EPSG code for data " + "native spatial reference system: " + nativeCrs); } else { targetSRSCode = "EPSG:" + code; } } catch (Exception e) { throw new ProcessException( "Failed to loookup an official EPSG code for " + "the source data native " + "spatial reference system", e); } } } } // import the data into the target store SimpleFeatureType targetType; try { targetType = importDataIntoStore(features, name, (DataStoreInfo) storeInfo); } catch (IOException e) { throw new ProcessException("Failed to import data into the target store", e); } // now import the newly created layer into GeoServer try { cb.setStore(storeInfo); // build the typeInfo and set CRS if necessary FeatureTypeInfo typeInfo = cb.buildFeatureType(targetType.getName()); if (targetSRSCode != null) { typeInfo.setSRS(targetSRSCode); } if (srsHandling != null) { typeInfo.setProjectionPolicy(srsHandling); } // compute the bounds cb.setupBounds(typeInfo); // build the layer and set a style LayerInfo layerInfo = cb.buildLayer(typeInfo); if (targetStyle != null) { layerInfo.setDefaultStyle(targetStyle); } catalog.add(typeInfo); catalog.add(layerInfo); return layerInfo.prefixedName(); } catch (Exception e) { throw new ProcessException("Failed to complete the import inside the GeoServer catalog", e); } } else if (coverage != null) { try { final File directory = catalog.getResourceLoader().findOrCreateDirectory("data", workspace, store); final File file = File.createTempFile(store, ".tif", directory); ((CoverageStoreInfo) storeInfo).setURL(file.toURL().toExternalForm()); ((CoverageStoreInfo) storeInfo).setType("GeoTIFF"); // check the target crs CoordinateReferenceSystem cvCrs = coverage.getCoordinateReferenceSystem(); String targetSRSCode = null; if (srs != null) { try { Integer code = CRS.lookupEpsgCode(srs, true); if (code == null) { throw new WPSException("Could not find a EPSG code for " + srs); } targetSRSCode = "EPSG:" + code; } catch (Exception e) { throw new ProcessException("Could not lookup the EPSG code for the provided srs", e); } } else { // check we can extract a code from the original data if (cvCrs == null) { // data is geometryless, we need a fake SRS targetSRSCode = "EPSG:4326"; srsHandling = ProjectionPolicy.FORCE_DECLARED; srs = DefaultGeographicCRS.WGS84; } else { CoordinateReferenceSystem nativeCrs = cvCrs; if (nativeCrs == null) { throw new ProcessException( "The original data has no native CRS, " + "you need to specify the srs parameter"); } else { try { Integer code = CRS.lookupEpsgCode(nativeCrs, true); if (code == null) { throw new ProcessException( "Could not find an EPSG code for data " + "native spatial reference system: " + nativeCrs); } else { targetSRSCode = "EPSG:" + code; srs = CRS.decode(targetSRSCode, true); } } catch (Exception e) { throw new ProcessException( "Failed to loookup an official EPSG code for " + "the source data native " + "spatial reference system", e); } } } } MathTransform tx = CRS.findMathTransform(cvCrs, srs); if (!tx.isIdentity() || !CRS.equalsIgnoreMetadata(cvCrs, srs)) { coverage = WCSUtils.resample( coverage, cvCrs, srs, null, Interpolation.getInstance(Interpolation.INTERP_NEAREST)); } GeoTiffWriter writer = new GeoTiffWriter(file); // setting the write parameters for this geotiff final ParameterValueGroup params = new GeoTiffFormat().getWriteParameters(); params .parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()) .setValue(DEFAULT_WRITE_PARAMS); final GeneralParameterValue[] wps = (GeneralParameterValue[]) params.values().toArray(new GeneralParameterValue[1]); try { writer.write(coverage, wps); } finally { try { writer.dispose(); } catch (Exception e) { // we tried, no need to fuss around this one } } // add or update the datastore info if (add) { catalog.add((CoverageStoreInfo) storeInfo); } else { catalog.save((CoverageStoreInfo) storeInfo); } cb.setStore((CoverageStoreInfo) storeInfo); AbstractGridCoverage2DReader reader = new GeoTiffReader(file); if (reader == null) { throw new ProcessException("Could not aquire reader for coverage."); } // coverage read params final Map customParameters = new HashMap(); /*String useJAIImageReadParam = "USE_JAI_IMAGEREAD"; if (useJAIImageReadParam != null) { customParameters.put(AbstractGridFormat.USE_JAI_IMAGEREAD.getName().toString(), Boolean.valueOf(useJAIImageReadParam)); }*/ CoverageInfo cinfo = cb.buildCoverage(reader, customParameters); // check if the name of the coverage was specified if (name != null) { cinfo.setName(name); } if (!add) { // update the existing CoverageInfo existing = catalog.getCoverageByCoverageStore( (CoverageStoreInfo) storeInfo, name != null ? name : coverage.getName().toString()); if (existing == null) { // grab the first if there is only one List<CoverageInfo> coverages = catalog.getCoveragesByCoverageStore((CoverageStoreInfo) storeInfo); if (coverages.size() == 1) { existing = coverages.get(0); } if (coverages.size() == 0) { // no coverages yet configured, change add flag and continue on add = true; } else { // multiple coverages, and one to configure not specified throw new ProcessException("Unable to determine coverage to configure."); } } if (existing != null) { cb.updateCoverage(existing, cinfo); catalog.save(existing); cinfo = existing; } } // do some post configuration, if srs is not known or unset, transform to 4326 if ("UNKNOWN".equals(cinfo.getSRS())) { // CoordinateReferenceSystem sourceCRS = // cinfo.getBoundingBox().getCoordinateReferenceSystem(); // CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326", true); // ReferencedEnvelope re = cinfo.getBoundingBox().transform(targetCRS, true); cinfo.setSRS("EPSG:4326"); // cinfo.setCRS( targetCRS ); // cinfo.setBoundingBox( re ); } // add/save if (add) { catalog.add(cinfo); LayerInfo layerInfo = cb.buildLayer(cinfo); if (styleName != null && targetStyle != null) { layerInfo.setDefaultStyle(targetStyle); } // JD: commenting this out, these sorts of edits should be handled // with a second PUT request on the created coverage /* String styleName = form.getFirstValue("style"); if ( styleName != null ) { StyleInfo style = catalog.getStyleByName( styleName ); if ( style != null ) { layerInfo.setDefaultStyle( style ); if ( !layerInfo.getStyles().contains( style ) ) { layerInfo.getStyles().add( style ); } } else { LOGGER.warning( "Client specified style '" + styleName + "'but no such style exists."); } } String path = form.getFirstValue( "path"); if ( path != null ) { layerInfo.setPath( path ); } */ boolean valid = true; try { if (!catalog.validate(layerInfo, true).isEmpty()) { valid = false; } } catch (Exception e) { valid = false; } layerInfo.setEnabled(valid); catalog.add(layerInfo); return layerInfo.prefixedName(); } else { catalog.save(cinfo); LayerInfo layerInfo = catalog.getLayerByName(cinfo.getName()); if (styleName != null && targetStyle != null) { layerInfo.setDefaultStyle(targetStyle); } return layerInfo.prefixedName(); } } catch (MalformedURLException e) { throw new ProcessException("URL Error", e); } catch (IOException e) { throw new ProcessException("I/O Exception", e); } catch (Exception e) { e.printStackTrace(); throw new ProcessException("Exception", e); } } return null; }