public MockCatalogBuilder dataStore(String name) { String dsId = newId(); final WorkspaceInfo ws = workspaces.peekLast(); final NamespaceInfo ns = namespaces.peekLast(); final DataStoreInfo ds = createNiceMock(DataStoreInfo.class); dataStores.add(ds); initStore(ds, DataStoreInfo.class, dsId, name, ws); // setup the property data store final File propDir = new File(dataDirRoot, name); HashMap cxParams = new HashMap(); cxParams.put(PropertyDataStoreFactory.DIRECTORY.key, propDir); cxParams.put(PropertyDataStoreFactory.NAMESPACE.key, ns.getURI()); expect(ds.getConnectionParameters()).andReturn(cxParams).anyTimes(); try { expect(ds.getDataStore(null)) .andAnswer( (IAnswer) new IAnswer<DataAccess>() { @Override public DataAccess answer() throws Throwable { return new PropertyDataStore(propDir, ns.getURI()); } }) .anyTimes(); } catch (IOException e) { } expect(catalog.getDataStore(dsId)).andReturn(ds).anyTimes(); expect(catalog.getDataStoreByName(name)).andReturn(ds).anyTimes(); expect(catalog.getDataStoreByName(ws.getName(), name)).andReturn(ds).anyTimes(); expect(catalog.getDataStoreByName(ws, name)).andReturn(ds).anyTimes(); ds.accept((CatalogVisitor) anyObject()); expectLastCall() .andAnswer( new VisitAnswer() { @Override protected void doVisit(CatalogVisitor visitor) { visitor.visit(ds); } }) .anyTimes(); callback.onStore(name, ds, ws, this); replay(ds); return this; }
/** * Create a repository with the given name for testing. * * @param name the repository name * @return a newly created {@link TestData} for the repository. * @throws Exception */ @Override protected TestData createRepo(String name) throws Exception { GeoGigTestData testData = new GeoGigTestData(); testData.setUp(name); testData.init(); GeoGIG geogig = testData.getGeogig(); Catalog catalog = helper.getCatalog(); CatalogBuilder catalogBuilder = testData.newCatalogBuilder(catalog); int i = rnd.nextInt(); catalogBuilder.namespace("geogig.org/" + i).workspace("geogigws" + i).store("geogigstore" + i); catalogBuilder.addAllRepoLayers().build(); String workspaceName = catalogBuilder.workspaceName(); String storeName = catalogBuilder.storeName(); DataStoreInfo dsInfo = catalog.getDataStoreByName(workspaceName, storeName); assertNotNull(dsInfo); assertEquals(GeoGigDataStoreFactory.DISPLAY_NAME, dsInfo.getType()); DataAccess<? extends FeatureType, ? extends Feature> dataStore = dsInfo.getDataStore(null); assertNotNull(dataStore); assertTrue(dataStore instanceof GeoGigDataStore); String repoId = (String) dsInfo.getConnectionParameters().get(GeoGigDataStoreFactory.REPOSITORY.key); RepositoryInfo repositoryInfo = RepositoryManager.get().get(repoId); assertNotNull(repositoryInfo); return new TestData(geogig); }
public static Optional<GeoGitDataStore> findDataStore(Request request, String repositoryName) { String[] wsds = repositoryName.split(":"); String workspace = wsds[0]; String datastore = wsds[1]; Catalog catalog = getCatalog(request); DataStoreInfo geogitStoreInfo = catalog.getDataStoreByName(workspace, datastore); if (null == geogitStoreInfo) { throw new RestletException( "No such repository: " + repositoryName, Status.CLIENT_ERROR_NOT_FOUND); } if (!geogitStoreInfo.isEnabled()) { throw new RestletException( "Repository is not enabled: " + repositoryName, Status.CLIENT_ERROR_BAD_REQUEST); } DataAccess<? extends FeatureType, ? extends Feature> dataStore; try { dataStore = geogitStoreInfo.getDataStore(null); } catch (IOException e) { throw new RestletException( "Error accessing datastore " + repositoryName, Status.SERVER_ERROR_INTERNAL, e); } if (!(dataStore instanceof GeoGitDataStore)) { throw new RestletException( repositoryName + " is not a Geogit DataStore: " + geogitStoreInfo.getType(), Status.CLIENT_ERROR_BAD_REQUEST); } GeoGitDataStore geogitDataStore = (GeoGitDataStore) dataStore; return Optional.of(geogitDataStore); }
private void configureGeogigDataStore() throws Exception { helper.insertAndAdd(helper.lines1); helper.getGeogig().command(CommitOp.class).call(); Catalog catalog = getCatalog(); CatalogFactory factory = catalog.getFactory(); NamespaceInfo ns = factory.createNamespace(); ns.setPrefix(WORKSPACE); ns.setURI(NAMESPACE); catalog.add(ns); WorkspaceInfo ws = factory.createWorkspace(); ws.setName(ns.getName()); catalog.add(ws); DataStoreInfo ds = factory.createDataStore(); ds.setEnabled(true); ds.setDescription("Test Geogig DataStore"); ds.setName(STORE); ds.setType(GeoGigDataStoreFactory.DISPLAY_NAME); ds.setWorkspace(ws); Map<String, Serializable> connParams = ds.getConnectionParameters(); Optional<URI> geogigDir = helper.getGeogig().command(ResolveGeogigURI.class).call(); File repositoryUrl = new File(geogigDir.get()).getParentFile(); assertTrue(repositoryUrl.exists() && repositoryUrl.isDirectory()); connParams.put(GeoGigDataStoreFactory.REPOSITORY.key, repositoryUrl); connParams.put(GeoGigDataStoreFactory.DEFAULT_NAMESPACE.key, ns.getURI()); catalog.add(ds); DataStoreInfo dsInfo = catalog.getDataStoreByName(WORKSPACE, STORE); assertNotNull(dsInfo); assertEquals(GeoGigDataStoreFactory.DISPLAY_NAME, dsInfo.getType()); DataAccess<? extends FeatureType, ? extends Feature> dataStore = dsInfo.getDataStore(null); assertNotNull(dataStore); assertTrue(dataStore instanceof GeoGigDataStore); FeatureTypeInfo fti = factory.createFeatureType(); fti.setNamespace(ns); fti.setCatalog(catalog); fti.setStore(dsInfo); fti.setSRS("EPSG:4326"); fti.setName("Lines"); fti.setAdvertised(true); fti.setEnabled(true); fti.setCqlFilter("INCLUDE"); fti.setProjectionPolicy(ProjectionPolicy.FORCE_DECLARED); ReferencedEnvelope bounds = new ReferencedEnvelope(-180, 180, -90, 90, CRS.decode("EPSG:4326")); fti.setNativeBoundingBox(bounds); fti.setLatLonBoundingBox(bounds); catalog.add(fti); fti = catalog.getFeatureType(fti.getId()); FeatureSource<? extends FeatureType, ? extends Feature> featureSource; featureSource = fti.getFeatureSource(null, null); assertNotNull(featureSource); }
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"); }
@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; }
/** * Adds a vector layer to the catalog setup. * * <p>The layer is created within a store named <code>qName.getPrefix()</code>, creating it if it * does not exist. The resulting store is a {@link PropertyDataStore} that points at the directory * <code>getDataDirectoryRoot()/qName.getPrefix()</code>. Similarily the layer and store are * created within a workspace named <code>qName.getPrefix()</code>, which is created if it does * not already exist. * * <p>The properties data for the layer is copied from the classpath, with a file name of "<code> * filename</code>.properties". The <tt>scope</tt> parameter is used as the class from which to * load the properties file relative to. * * <p>The <tt>props</tt> parameter is used to define custom properties for the layer. See the * {@link LayerProperty} class for supported properties. */ public void addVectorLayer( QName qName, Map<LayerProperty, Object> props, String filename, Class scope, Catalog catalog) throws IOException { String prefix = qName.getPrefix(); String name = qName.getLocalPart(); String uri = qName.getNamespaceURI(); // configure workspace if it doesn;t already exist if (catalog.getWorkspaceByName(prefix) == null) { addWorkspace(prefix, uri, catalog); } // configure store if it doesn't already exist File storeDir = catalog.getResourceLoader().findOrCreateDirectory(prefix); DataStoreInfo store = catalog.getDataStoreByName(prefix); if (store == null) { store = catalog.getFactory().createDataStore(); store.setName(prefix); store.setWorkspace(catalog.getWorkspaceByName(prefix)); store.setEnabled(true); store.getConnectionParameters().put(PropertyDataStoreFactory.DIRECTORY.key, storeDir); store.getConnectionParameters().put(PropertyDataStoreFactory.NAMESPACE.key, uri); catalog.add(store); } // copy the properties file over catalog.getResourceLoader().copyFromClassPath(filename, new File(storeDir, filename), scope); // configure feature type FeatureTypeInfo featureType = catalog.getFactory().createFeatureType(); featureType.setStore(store); featureType.setNamespace(catalog.getNamespaceByPrefix(prefix)); featureType.setName(LayerProperty.NAME.get(props, name)); featureType.setNativeName(FilenameUtils.getBaseName(filename)); featureType.setTitle(name); featureType.setAbstract("abstract about " + name); Integer srs = LayerProperty.SRS.get(props, SRS.get(qName)); if (srs == null) { srs = 4326; } featureType.setSRS("EPSG:" + srs); try { featureType.setNativeCRS(CRS.decode("EPSG:" + srs)); } catch (Exception e) { LOGGER.warning("Failed to decode EPSG:" + srs + ", setting the native SRS to null"); } featureType.setNumDecimals(8); featureType.getKeywords().add(new Keyword(name)); featureType.setEnabled(true); featureType.setProjectionPolicy( LayerProperty.PROJECTION_POLICY.get(props, ProjectionPolicy.NONE)); featureType.setLatLonBoundingBox( LayerProperty.LATLON_ENVELOPE.get(props, DEFAULT_LATLON_ENVELOPE)); featureType.setNativeBoundingBox(LayerProperty.ENVELOPE.get(props, null)); FeatureTypeInfo ft = catalog.getFeatureTypeByDataStore(store, name); LayerInfo layer = catalog.getLayerByName(new NameImpl(prefix, name)); if (ft == null) { ft = featureType; catalog.add(featureType); } else { if (layer == null) { // handles the case of layer removed, but feature type not catalog.remove(ft); ft = featureType; catalog.add(featureType); } else { new CatalogBuilder(catalog).updateFeatureType(ft, featureType); catalog.save(ft); } } if (layer == null || !layer.getResource().getNamespace().equals(catalog.getNamespaceByPrefix(prefix))) { layer = catalog.getFactory().createLayer(); } layer.setResource(ft); StyleInfo defaultStyle = null; if (LayerProperty.STYLE.get(props, null) != null) { defaultStyle = catalog.getStyleByName(LayerProperty.STYLE.get(props, null)); } else { // look for a style matching the layer name defaultStyle = catalog.getStyleByName(name); if (defaultStyle == null) { // see if the resource exists and we just need to create it if (getClass().getResource(name + ".sld") != null) { addStyle(name, catalog); defaultStyle = catalog.getStyleByName(name); } } } if (defaultStyle == null) { defaultStyle = catalog.getStyleByName(DEFAULT_VECTOR_STYLE); } layer.getStyles().clear(); layer.setDefaultStyle(defaultStyle); layer.setType(LayerInfo.Type.VECTOR); layer.setEnabled(true); if (layer.getId() == null) { catalog.add(layer); } else { catalog.save(layer); } }