Skip to content

Commit

Permalink
Make uploading to geoserver identical for both shapefile and geopacka…
Browse files Browse the repository at this point in the history
…ge (#266)

* Make uploading to geoserver identical for both shapefile and geopackage

* updated changelog

* added copyright header

* added copyright header

* Added log for error message

* removed printout statements
  • Loading branch information
ywkim312 committed Feb 1, 2024
1 parent 08a7599 commit 11cee61
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Add `hazardDatasets` field to TornadoDataset, TornadoModel and EarthquakeModel class [#213](https://github.com/IN-CORE/incore-services/issues/213)
- Renaming option for uploading geoserver layers [#259](https://github.com/IN-CORE/incore-services/issues/259)
- GeoPakage handling in services [#205](https://github.com/IN-CORE/incore-services/issues/205)
- Raster GeoPackage checker for not supporting it [#258](https://github.com/IN-CORE/incore-services/issues/258)
- `hazardDatasets` field to TornadoDataset, TornadoModel and EarthquakeModel class [#213](https://github.com/IN-CORE/incore-services/issues/213)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*******************************************************************************
* Copyright (c) 2024 University of Illinois and others. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Mozilla Public License v2.0 which accompanies this distribution,
* and is available at https://www.mozilla.org/en-US/MPL/2.0/
*
* Contributors:
* Yong Wook Kim (NCSA) - initial API and implementation
*******************************************************************************/
package edu.illinois.ncsa.incore.service.data.utils;

import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -129,7 +138,6 @@ public boolean postFileToGeoserver(String apiUrl, File inData, String contType)
return published;
} catch (Exception e) {
logger.error("Failed to POST to geoserver rest api", e);
System.out.println(e);
return false;
}
}
Expand All @@ -140,51 +148,79 @@ public boolean postFileToGeoserver(String apiUrl, File inData, String contType)
* @param store
* @param inFile
* @param inExt
* @param renameLayer
* @return
*/
public boolean uploadToGeoserver(String store, File inFile, String inExt) {
public boolean uploadToGeoserver(String store, File inFile, String inExt, Boolean renameLayer) {
// in here, there are two ways to upload file to geoserver
// 1. rename the layer name different from the file name
// 2. upload the file with the original file name so the layer name becomes the file name
// when the layer name needs to be renamed, the steps should be:
// 1. create datastore with the file (shapefile or geopackage)
// 2. create layer with xml to control the name to be changed to dataset id.
// the reason of using one method that is for renaming one can't be sued for the both cases is because
// when the datastore created with the file name and create layer without renaming it,
// createLayer method will make an error in GeoServer side, complaining the data with the name already exists
// so there should be two different methods in uploading, renaming and not renaming

String fileName = FilenameUtils.getBaseName(inFile.getName());

// check if workspace exists
boolean created = createWorkspace(this.geoserverUrl, GEOSERVER_WORKSPACE);
boolean published = false;

if (inExt.equalsIgnoreCase("shp")) {
String restUrl = this.geoserverUrl + "/rest/workspaces/" + GEOSERVER_WORKSPACE + "/datastores/" + store + "/file.shp";
published = this.postFileToGeoserver(restUrl, inFile, "zip");
if (renameLayer) {
published = this.uploadToGeoserverWithRenaming(fileName, store, inFile, "shapefile");
} else {
String restUrl = this.geoserverUrl + "/rest/workspaces/" + GEOSERVER_WORKSPACE + "/datastores/" + store + "/file.shp";
published = this.postFileToGeoserver(restUrl, inFile, "zip");
}
} else if (inExt.equalsIgnoreCase("gpkg")) {
// geopackage should have different steps due to its renaming convention in IN-CORE
// since IN-CORE uses the dataset id as a file name in geoserver, the geopackage name needs to be renamed to dataset id
// to rename, the POST action should create datastore first with the geopackage data,
// then create layer with xml to control the name to be changed to dataset id.
// if you don't need to rename, set renameData boolean in following line to false
Boolean renameData = true;
if (renameData) {
// create datastore then create layer
try {
String restUrl = this.geoserverUrl + "/rest";
String fileNameNoExt = fileName.split("\\.")[0];
int datastoreResponse = createDatastore(restUrl, GEOSERVER_WORKSPACE, store, inFile.getAbsolutePath(), "geopackage");
int layerResponse = createLayer(restUrl, GEOSERVER_WORKSPACE, store, store, fileNameNoExt);
if (datastoreResponse == 201 && layerResponse == 201) {
published = true;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
if (renameLayer) {
published = this.uploadToGeoserverWithRenaming(fileName, store, inFile, "geopackage");
} else{
String restUrl = this.geoserverUrl + "/rest/workspaces/" + GEOSERVER_WORKSPACE + "/datastores/" + store +
"/file.gpkg?configure=all&name=" + store;
published = this.postFileToGeoserver(restUrl, inFile, "gpkg");
}
} else if (inExt.equalsIgnoreCase("tif")) {
// currently, renaming of the raster layer is not being supported
String restUrl = this.geoserverUrl + "/rest/workspaces/" + GEOSERVER_WORKSPACE + "/coveragestores/" + store + "/file.geotiff";
published = this.postFileToGeoserver(restUrl, inFile, "tif");
}

return published;
}

/**
* upload file to geoserver with renaming
* this method is for the case when the file name is different from published layer name
*
* @param fileName
* @param store
* @param inFile
* @param fileForamt
* @return
*/
public Boolean uploadToGeoserverWithRenaming(String fileName, String store, File inFile, String fileForamt) {
Boolean published = false;
try {
String restUrl = this.geoserverUrl + "/rest";
String fileNameNoExt = fileName.split("\\.")[0];
int datastoreResponse = createDatastore(restUrl, GEOSERVER_WORKSPACE, store, inFile.getAbsolutePath(), fileForamt);
int layerResponse = createLayer(restUrl, GEOSERVER_WORKSPACE, store, store, fileNameNoExt);
if (datastoreResponse == 201 && layerResponse == 201) {
published = true;
}
} catch (IOException e) {
logger.error("Failed to upload to geoserver with renaming", e);
throw new RuntimeException(e);
}

return published;
}

/**
* XML template for feature
*
Expand Down Expand Up @@ -410,7 +446,6 @@ public boolean deleteStoreFromGeoserver(String store) {
}
} catch (Exception e) {
logger.error("Failed to Delete to geoserver rest api", e);
System.out.println(e);
return false;
}
return isDeleted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public static boolean datasetUploadToGeoserver(Dataset dataset, IRepository repo
double[] bbox = GeotoolsUtils.getBboxFromShp(new File(fileName));
dataset.setBoundingBox(bbox);
repository.addDataset(dataset);
published = gsApi.uploadToGeoserver(datasetId, outFile, inExt);
// layer in geoserver doesn't have to be renamed
published = gsApi.uploadToGeoserver(datasetId, outFile, inExt, false);
} else if (isTif == true || isAsc == true) {
if (isTif) {
inExt = "tif";
Expand All @@ -70,7 +71,8 @@ public static boolean datasetUploadToGeoserver(Dataset dataset, IRepository repo
double[] bbox = GeotoolsUtils.getBboxFromGrid(outFile);
dataset.setBoundingBox(bbox);
repository.addDataset(dataset);
published = gsApi.uploadToGeoserver(datasetId, outFile, inExt);
// layer in geoserver doesn't have to be renamed
published = gsApi.uploadToGeoserver(datasetId, outFile, inExt, false);
}
}

Expand Down Expand Up @@ -100,8 +102,9 @@ public static boolean networkDatasetUploadToGeoserver(Dataset dataset, IReposito
double[] bbox = GeotoolsUtils.getBboxFromShp(new File(linkFileName));
dataset.setBoundingBox(bbox);
repository.addDataset(dataset);
link_published = gsApi.uploadToGeoserver(datasetId, outFiles[0], inExt);
node_published = gsApi.uploadToGeoserver(datasetId, outFiles[1], inExt);
// layer in geoserver doesn't have to be renamed
link_published = gsApi.uploadToGeoserver(datasetId, outFiles[0], inExt, false);
node_published = gsApi.uploadToGeoserver(datasetId, outFiles[1], inExt, false);

}

Expand Down Expand Up @@ -131,7 +134,8 @@ public static boolean removeStoreFromGeoserver(String id) {
*/
public static boolean uploadGpkgToGeoserver(String store, File gpkgFile) {
GeoserverRestApi gsApi = GeoserverRestApi.createGeoserverApi();
boolean isPublished = gsApi.uploadToGeoserver(store, gpkgFile, "gpkg");
// layer in geoserver doesn't have to be renamed to dataset id
boolean isPublished = gsApi.uploadToGeoserver(store, gpkgFile, "gpkg", true);

return isPublished;
}
Expand Down

0 comments on commit 11cee61

Please sign in to comment.