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

Handle empty geopackage geometries #561

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -25,11 +25,14 @@
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.operation.MathTransform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility that reads {@link SourceFeature SourceFeatures} from the vector geometries contained in a GeoPackage file.
*/
public class GeoPackageReader extends SimpleReader<SimpleFeature> {
private static final Logger LOGGER = LoggerFactory.getLogger(GeoPackageReader.class);

private final boolean keepUnzipped;
private Path extractedPath = null;
Expand Down Expand Up @@ -122,6 +125,7 @@ public long getFeatureCount() {
public void readFeatures(Consumer<SimpleFeature> next) throws Exception {
var latLonCRS = CRS.decode("EPSG:4326");
long id = 0;
boolean loggedMissingGeometry = false;

for (var featureName : geoPackage.getFeatureTables()) {
FeatureDao features = geoPackage.getFeatureDao(featureName);
Expand All @@ -136,11 +140,16 @@ public void readFeatures(Consumer<SimpleFeature> next) throws Exception {

for (var feature : features.queryForAll()) {
GeoPackageGeometryData geometryData = feature.getGeometry();
if (geometryData == null) {
byte[] wkb;
if (geometryData == null || (wkb = geometryData.getWkb()).length == 0) {
if (!loggedMissingGeometry) {
loggedMissingGeometry = true;
LOGGER.warn("Geopackage file contains empty geometry: {}", geoPackage.getPath());
}
continue;
}

Geometry featureGeom = (new WKBReader()).read(geometryData.getWkb());
Geometry featureGeom = (new WKBReader()).read(wkb);
Geometry latLonGeom = (transform.isIdentity()) ? featureGeom : JTS.transform(featureGeom, transform);

FeatureColumns columns = feature.getColumns();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.onthegomap.planetiler.TestUtils;
import com.onthegomap.planetiler.collection.IterableOnce;
import com.onthegomap.planetiler.geo.GeoUtils;
import com.onthegomap.planetiler.stats.Stats;
import com.onthegomap.planetiler.util.FileUtils;
Expand All @@ -13,7 +12,8 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -45,9 +45,7 @@ void testReadGeoPackage(boolean keepUnzipped) throws IOException {
List<Geometry> points = new ArrayList<>();
List<String> names = new ArrayList<>();
WorkerPipeline.start("test", Stats.inMemory())
.readFromTiny("files", List.of(Path.of("dummy-path")))
.addWorker("geopackage", 1,
(IterableOnce<Path> p, Consumer<SimpleFeature> next) -> reader.readFeatures(next))
.fromGenerator("geopackage", reader::readFeatures, 1)
.addBuffer("reader_queue", 100, 1)
.sinkToConsumer("counter", 1, elem -> {
assertTrue(elem.getTag("name") instanceof String);
Expand All @@ -67,4 +65,27 @@ void testReadGeoPackage(boolean keepUnzipped) throws IOException {
}
}
}

@Test
@Timeout(30)
void testReadEmptyGeoPackage() throws IOException {
Path path = TestUtils.pathToResource("empty-geom.gpkg");

try (
var reader = new GeoPackageReader(null, "test", path, tmpDir, false)
) {
for (int iter = 0; iter < 2; iter++) {
String id = "iter=" + iter;
assertEquals(1, reader.getFeatureCount(), id);
AtomicInteger found = new AtomicInteger(0);
WorkerPipeline.start("test", Stats.inMemory())
.fromGenerator("geopackage", reader::readFeatures, 1)
.addBuffer("reader_queue", 100, 1)
.sinkToConsumer("counter", 1, elem -> {
found.incrementAndGet();
}).await();
assertEquals(0, found.get());
}
}
}
}
Binary file not shown.