Skip to content

Commit

Permalink
feat: shapefile writer exports valid shapefiles for mappings includin…
Browse files Browse the repository at this point in the history
…g feature types with no geometry

Export valid shapefiles, including .cpg, for feature types with geometries.
Displays a warning that feature types with no geometry will not be exported to shapefile.

ING-3974
  • Loading branch information
emanuelaepure10 committed Aug 14, 2023
1 parent 019cb4e commit 38bb2cb
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ShapefileInstanceWriterTest {
IOReport report = writer.execute(new LogProgressIndicator())

assertTrue(report.isSuccess())
assertFalse(report.getErrors().isEmpty())
assertFalse(report.getWarnings().isEmpty())

return report;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ Import-Package: com.google.common.io;version="9.0.0",
eu.esdihumboldt.util
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.7.0",
org.eclipse.ui;bundle-version="3.7.0",
eu.esdihumboldt.hale.io.shp;bundle-version="2.5.0"
eu.esdihumboldt.hale.io.shp;bundle-version="2.5.0",
org.geotools,
eu.esdihumboldt.hale.ui,
eu.esdihumboldt.hale.common.schema,
org.opengis
Automatic-Module-Name: eu.esdihumboldt.hale.io.shp.ui
2 changes: 1 addition & 1 deletion io/plugins/eu.esdihumboldt.hale.io.shp.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</provider>
</configPage>
<configPage
class="eu.esdihumboldt.hale.ui.io.config.SimpleTargetCRSConfigurationPage"
class="eu.esdihumboldt.hale.io.shp.ui.ShapeTargetCRSConfigurationPage"
order="0">
<provider
ref="eu.esdihumboldt.hale.io.shp.instance.writer">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2020 wetransform GmbH
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution. If not, see <http://www.gnu.org/licenses/>.
*
* Contributors:
* wetransform GmbH <http://www.wetransform.to>
*/

package eu.esdihumboldt.hale.io.shp.ui;

import eu.esdihumboldt.hale.common.core.io.IOProvider;
import eu.esdihumboldt.hale.common.instance.io.GeoInstanceWriter;
import eu.esdihumboldt.hale.ui.io.IOWizard;
import eu.esdihumboldt.hale.ui.io.config.SimpleTargetCRSConfigurationPage;

/**
* Configuration page for the Shape UI.
*
* @param <W> the concrete I/O wizard type
* @param <P> the {@link IOProvider} type used in the wizard
*
* @author Emanuela Epure
*/
public class ShapeTargetCRSConfigurationPage<P extends GeoInstanceWriter, W extends IOWizard<P>>
extends SimpleTargetCRSConfigurationPage<P, W> {

/**
* Default constructor.
*/
public ShapeTargetCRSConfigurationPage() {
super(true);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ public class TypeSelectionPage extends InstanceReaderConfigurationPage
implements ShapefileConstants {

private TypeDefinitionSelector selector;

private LocatableInputSupplier<? extends InputStream> lastSource;

private TypeDefinition lastType;

private Button matchShortPropertyNames;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ protected IOReport execute(ProgressIndicator progress, IOReporter reporter)
return file.toURI();
}).collect(Collectors.toList());

// Reset the target property so that a caller can find out which
// Reset the target property so that a caller can find out
// which
// files were created.
setTarget(new MultiLocationOutputSupplier(uris));
}
Expand Down Expand Up @@ -152,7 +153,7 @@ protected String getDefaultTypeName() {
* 3. create features <br>
* 4. write the feature data to the Shapefile.
*
* @param instances instance to write to.
* @param instanceCollection instance to write to.
* @param progress the progress indicator.
* @param reporter the reporter.
* @param location file path URI.
Expand All @@ -162,19 +163,19 @@ protected String getDefaultTypeName() {
* @throws IOException exception in any.
*
*/
protected List<String> writeInstances(InstanceCollection instances, ProgressIndicator progress,
IOReporter reporter, URI location) throws IOException {
protected List<String> writeInstances(InstanceCollection instanceCollection,
ProgressIndicator progress, IOReporter reporter, URI location) throws IOException {

// in all the variables, outer Map is for tracking multiple schemas and
// inner Map for multiple geometries.
Map<String, Map<String, SimpleFeatureType>> schemaFtMap = createFeatureType(instances,
progress, reporter);
Map<String, Map<String, SimpleFeatureType>> schemaFtMap = createFeatureType(
instanceCollection, progress, reporter);

Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap = createSchema(location,
schemaFtMap);

Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap = createFeatures(instances,
progress, reporter, schemaFtMap);
Map<String, Map<String, List<SimpleFeature>>> schemaFeaturesMap = createFeatures(
instanceCollection, progress, reporter, schemaFtMap);

return writeToFile(schemaDataStoreMap, schemaFtMap, schemaFeaturesMap);
}
Expand Down Expand Up @@ -226,13 +227,16 @@ private Map<String, Map<String, SimpleFeatureType>> createFeatureType(
// present.
}
}
for (String localPart : missingGeomsForSchemas) {
reporter.error(
"Cannot create Shapefile for Schema: " + localPart + " as no Geometry found!!");
}

// create SimpleFeatureType from SimpleFeatureTypeBuilder.
for (Entry<String, Map<String, SimpleFeatureTypeBuilder>> schemaEntry : schemaBuilderMap
.entrySet()) {

if (missingGeomsForSchemas.contains(schemaEntry.getKey())) {
reporter.warn("No geometry found for " + schemaEntry.getKey());
continue;
}

for (Entry<String, SimpleFeatureTypeBuilder> geometryEntry : schemaEntry.getValue()
.entrySet()) {
SimpleFeatureType buildFeatureType = geometryEntry.getValue().buildFeatureType();
Expand Down Expand Up @@ -439,9 +443,6 @@ private void createSimpleFeatureTypeBuilderWithGeometry(String localPart,
private Map<String, Map<String, ShapefileDataStore>> createSchema(URI location,
Map<String, Map<String, SimpleFeatureType>> schemaSftMap) throws IOException {

if (schemaSftMap.isEmpty()) {
throw new IOException("Cannot export to the shape file as no Geometry found!!");
}
Map<String, Map<String, ShapefileDataStore>> schemaDataStoreMap = new HashMap<String, Map<String, ShapefileDataStore>>();

// logic to create file name based on the multiple schemas and/or
Expand Down Expand Up @@ -635,8 +636,15 @@ private void writePropertiesInstanceData(
List<GeometryProperty<?>> geoms = traverseInstanceForGeometries(instance);
// add value by traversing geometryType from instance
for (GeometryProperty<?> geoProp : geoms) {
String geometryType = geoProp.getGeometry().getGeometryType();
schemaFbMap.get(localPart).get(geometryType).add(value);
if (geoProp.getGeometry() != null) {
String geometryType = geoProp.getGeometry().getGeometryType();
if (schemaFbMap.get(localPart) != null
&& schemaFbMap.get(localPart).get(geometryType) != null
&& value != null) {
schemaFbMap.get(localPart).get(geometryType).add(value);
}
}

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;
import org.geotools.referencing.CRS;

import eu.esdihumboldt.hale.common.core.io.IOProvider;
Expand All @@ -49,19 +51,39 @@ public class SimpleTargetCRSConfigurationPage<P extends GeoInstanceWriter, W ext
private Button checkConvert;
private Button selectCrs;
private CRSDefinition crsDef;
private boolean showWarning = false;

/**
* Default constructor.
*/
public SimpleTargetCRSConfigurationPage() {
super("targetCRS");

setDefaultDescription();
}

/**
* method to be called from different constructors
*/
private void setDefaultDescription() {
setTitle("Coordinate reference system");
setDescription("Configure the target coordinate reference system");

setPageComplete(false);
}

/**
* @param showWarning parameter that shows the warning of exporting feature
* types without geometry
*/
public SimpleTargetCRSConfigurationPage(boolean showWarning) {
super("targetCRS");

this.showWarning = showWarning;

setDefaultDescription();
}

@Override
public void enable() {
// nothing to do
Expand Down Expand Up @@ -138,6 +160,16 @@ public void widgetSelected(SelectionEvent e) {

});

if (showWarning) {
Label separatorLabel = new Label(page, SWT.NONE);
separatorLabel.setText("Warning! Feature types with no geometry will not be exported");

// Set the text colour of the label to yellow
Color yellow = PlatformUI.getWorkbench().getDisplay()
.getSystemColor(SWT.COLOR_DARK_GRAY);
separatorLabel.setForeground(yellow);
}

// only update on first show
// update();
}
Expand Down

0 comments on commit 38bb2cb

Please sign in to comment.