Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recalculate Bounding Boxes #200

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3206,4 +3206,123 @@ public void postImport(int i) throws Exception {
importerManager.postImport(i);
}

/**
* Defines multiple modes of recalculating bounding boxes.
*
* @author Carl Schroedl - [email protected]
*/
public enum BBoxRecalculationMode {
/**
* Recalculate the native bounding box, but do not recalculate the
* lat/long bounding box.
*/
NATIVE_BBOX("nativebbox"),

/**
* Recalculate both the native bounding box and the lat/long bounding
* box.
*/
NATIVE_AND_LAT_LON_BBOX("nativebbox,latlonbbox"),

/**
* Do not calculate any fields, regardless of the projection, projection
* policy, etc. This might be useful to avoid slow recalculation when
* operating against large datasets.
*/
NONE(""),
;

private final String paramValue;

/**
* Associates the enum value with a URL query string parameter value
* @param paramValue
*/
BBoxRecalculationMode(String paramValue){
this.paramValue = paramValue;
}

/**
* Get the URL param value
* @return The query string parameter value
*/
public String getParamValue(){
return paramValue;
}
}

/**
*
* Recalculate the bounding box for a feature type or a coverage
*
* @param type
* @param xmlElementName - either featureType or coverage
* @param workspace
* @param storeName
* @param layerName
* @param calculationMode
* @return true if recalculation succeeded, false otherwise.
*/
private boolean recalculateBBox(StoreType type, GSResourceEncoder renc, String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode){

String baseUrl = restURL + "/rest/workspaces/" + workspace + "/" +
type.getType().toLowerCase() +"s/" + storeName + "/" +
type.getTypeName().toLowerCase() + "/" +
layerName + "." + Format.XML.toString();


// LOGGER.debug("Retrieving current state of item from "+ baseUrl);
// String getResult = HTTPUtils.get(baseUrl, gsuser, gspass);

// LOGGER.debug("Current state of item is:\n" + getResult);

String sUrl = baseUrl + "?recalculate=" + calculationMode.getParamValue();
LOGGER.debug("Constructed the following url for bounding box recalculation: " + sUrl);

// String body = getResult
// GSWorkspaceEncoder wsenc = new GSWorkspaceEncoder(workspace);

// String body = wsenc.toString();
// String body = "<" + xmlElementName +"><name>" + layerName + "</name>" +
// "<enabled>" + enabled + "</enabled></" + xmlElementName + ">";
renc.remove(GSResourceEncoder.KEYWORDS);
renc.remove(GSResourceEncoder.METADATA);
renc.remove(GSResourceEncoder.METADATALINKS);
String body = renc.toString();
Copy link
Author

@cschroedl-gov cschroedl-gov Sep 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: never mind! After adding a method to XmlElement to recursively delete empty children, I was able to use the GS*Encoder classes instead of building the xml with Strings.

When I switched to using GSEncoder classes, I noted that several extra elements were added to the resulting XML documents. These extra empty elements could overwrite existing keywords, metadata, and metadatalinks. While I can remove a hard-coded list of extra elements now, the same kind of problem can occur in the future if new fields are added to the encoders without updating this class. I opted to revert to simple String construction instead of GSEncoder classes to ensure that this functionality would continue to be stable in the future.

String sendResult = HTTPUtils.putXml(sUrl, body, gsuser, gspass);
boolean success = sendResult != null;
return success;
}

/**
* Recalculate a bounding box for a feature type
* @param workspace
* @param storeName
* @param layerName
* @param calculationMode
* @return true if successful, false otherwise
*/
public boolean recalculateFeatureTypeBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode, boolean enabled){
GSFeatureTypeEncoder fenc = new GSFeatureTypeEncoder();
fenc.remove(GSFeatureTypeEncoder.ATTRIBUTES);
fenc.setName(layerName);
fenc.setEnabled(enabled);
return recalculateBBox(StoreType.DATASTORES, fenc, workspace, storeName, layerName, calculationMode);
}

/**
* Recalculate a bounding box for a coverage
* @param workspace
* @param storeName
* @param layerName
* @param calculationMode
* @return true if successful, false otherwise
*/
public boolean recalculateCoverageBBox(String workspace, String storeName, String layerName, BBoxRecalculationMode calculationMode, boolean enabled){
GSCoverageEncoder cenc = new GSCoverageEncoder();
cenc.remove(GSCoverageEncoder.SUPPORTED_FORMATS);
cenc.setName(layerName);
cenc.setEnabled(enabled);
return recalculateBBox(StoreType.COVERAGESTORES, cenc, workspace, storeName, layerName, calculationMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GSCoverageEncoder extends GSResourceEncoder {
public final static String NATIVECOVERAGENAME = "nativeCoverageName";

private final static String NATIVE_FORMAT="nativeFormat";
private final static String SUPPORTED_FORMATS="supportedFormats";
public final static String SUPPORTED_FORMATS="supportedFormats";

private final static String REQUEST_SRS="requestSRS";
private final static String RESPONSE_SRS="responseSRS";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* GeoServer-Manager - Simple Manager Library for GeoServer
*
* Copyright (C) 2007,2011 GeoSolutions S.A.S.
* http://www.geo-solutions.it
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package it.geosolutions.geoserver.rest.publisher;

import it.geosolutions.geoserver.rest.GeoServerRESTPublisher;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.StoreType;
import it.geosolutions.geoserver.rest.GeoServerRESTPublisher.UploadMethod;
import it.geosolutions.geoserver.rest.GeoserverRESTTest;
import it.geosolutions.geoserver.rest.decoder.RESTLayer;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder.ProjectionPolicy;
import it.geosolutions.geoserver.rest.encoder.coverage.GSCoverageEncoderTest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.httpclient.NameValuePair;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

/**
* Testcase for publishing layers on geoserver.
* We need a running GeoServer to properly run the tests.
* If such geoserver instance cannot be contacted, tests will be skipped.
*
* @author Carl Schroedl - [email protected]
*/
public class GeoserverRESTRecalculateTest extends GeoserverRESTTest {

private final static Logger LOGGER = LoggerFactory.getLogger(GeoserverRESTRecalculateTest.class);

@Before
@Override
public void before(){
super.before();
deleteAll();
assertTrue(publisher.createWorkspace(DEFAULT_WS));
}

@After
public void cleanUp(){
deleteAll();
}

@Test
public void testRecalculateFeatureTypeBBox() throws FileNotFoundException, IOException {
if (!enabled())
return;

String storeName = "resttestshp";
String layerName = "cities";

File zipFile = new ClassPathResource("testdata/resttestshp.zip").getFile();
assertTrue(publisher.publishShp(DEFAULT_WS, storeName, layerName, zipFile));

for(GeoServerRESTPublisher.BBoxRecalculationMode recalcMode : GeoServerRESTPublisher.BBoxRecalculationMode.values()){
boolean recalculated = publisher.recalculateFeatureTypeBBox(DEFAULT_WS,storeName, layerName, recalcMode, true);
assertTrue("recalculateBBox failed with recalculation mode '" +recalcMode.toString() + "'.", recalculated);
}
}

@Test
public void testRecalculateCoverageBBox() throws FileNotFoundException, IOException {
if (!enabled())
return;

String storeName = "testRESTStoreArcGrid";
String layerName = "resttestdem";

File arcgrid = new ClassPathResource("testdata/resttestdem.asc").getFile();

assertTrue(publisher.publishArcGrid(DEFAULT_WS, storeName, layerName, arcgrid));

for(GeoServerRESTPublisher.BBoxRecalculationMode recalcMode : GeoServerRESTPublisher.BBoxRecalculationMode.values()){
boolean recalculated = publisher.recalculateCoverageBBox(DEFAULT_WS,storeName, layerName, recalcMode, true);
assertTrue("recalculateBBox failed with recalculation mode '" +recalcMode.toString() + "'.", recalculated);
}
}
}