github-code-scanning[bot] commented on code in PR #780: URL: https://github.com/apache/incubator-baremaps/pull/780#discussion_r1322729351
########## baremaps-core/src/main/java/org/apache/baremaps/geocoderosm/GeocoderOSMDocumentMapper.java: ########## @@ -0,0 +1,98 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.baremaps.geocoderosm; + + + +import java.util.function.Function; +import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.model.Node; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.LatLonShape; +import org.apache.lucene.document.NumericDocValuesField; +import org.apache.lucene.document.StoredField; +import org.apache.lucene.document.TextField; +import org.apache.lucene.geo.Polygon; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.geojson.GeoJsonWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class GeocoderOSMDocumentMapper implements Function<Element, Document> { + private static final Logger logger = LoggerFactory.getLogger(GeocoderOSMDocumentMapper.class); + + @Override + public Document apply(Element element) { + var document = new Document(); + document.add(new StoredField("osm_id", element.id())); + document.add(new StoredField("osm_type", element.getClass().getSimpleName())); + + if (element.getTags().containsKey(OSMTags.NAME.key())) { + document.add( + new TextField(OSMTags.NAME.key(), element.getTags().get(OSMTags.NAME.key()).toString(), + Field.Store.YES)); + } + + if (element instanceof Node node) { + document.add(LatLonShape.createIndexableFields("polygon", node.getLat(), node.getLon())[0]); + document.add(new StoredField("latitude", node.getLat())); + document.add(new StoredField("longitude", node.getLon())); + } + if (element.getGeometry() != null + && element.getGeometry().getGeometryType().equals(Geometry.TYPENAME_LINESTRING)) { + logger.debug("Geometry linestring ignored as not supported by Lucene Polygon.fromGeoJson: {}", + element); + } + if (element.getGeometry() != null + && !element.getGeometry().getGeometryType().equals(Geometry.TYPENAME_POINT) + && !element.getGeometry().getGeometryType().equals(Geometry.TYPENAME_LINESTRING)) { + // JTS to GeoJSON + var geojsonWriter = new GeoJsonWriter(); + // Remove crs field in GeoJSON as Lucene parsing is very strict. + // Avoid "crs must be CRS84 from OGC, but saw: EPSG:4326" + // See: + // https://github.com/apache/lucene/blob/ef42af65f27f7f078b1ab426de9f2b2fa214ad86/lucene/core/src/java/org/apache/lucene/geo/SimpleGeoJSONPolygonParser.java#L180 + geojsonWriter.setEncodeCRS(false); + // Assume that Geometry is in EPSG:4326/WGS84 for Lucene Polygon.fromGeoJSON + var geojson = geojsonWriter.write(element.getGeometry()); + + // GeoJSON to Lucene Polygon + try { + var polygons = Polygon.fromGeoJSON(geojson); + + for (Polygon polygon : polygons) { + // LatLonShape.createIndexableFields can create multiple polygons out of a single polygon + // through tesselation + for (Field field : LatLonShape.createIndexableFields("polygon", polygon)) { + document.add(field); + } + } + } catch (Exception e) { + // ignore geometry + logger.debug("Geometry ({}) failed indexing caused by: {}", + element, e); + } + + } + + if (element.getTags().containsKey(OSMTags.POPULATION.key())) { + var population = Long.parseLong(element.getTags().get(OSMTags.POPULATION.key()).toString()); Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/802) ########## baremaps-core/src/test/java/org/apache/baremaps/geocoderosm/OSMIndexTest.java: ########## @@ -0,0 +1,162 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.apache.baremaps.geocoderosm; + +import static org.apache.baremaps.testing.TestFiles.LIECHTENSTEIN_OSM_PBF; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.apache.baremaps.utils.FileUtils; +import org.apache.baremaps.workflow.WorkflowContext; +import org.apache.baremaps.workflow.tasks.CreateGeocoderOpenStreetMap; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.LatLonShape; +import org.apache.lucene.document.ShapeField; +import org.apache.lucene.geo.Polygon; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.FieldExistsQuery; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.SearcherFactory; +import org.apache.lucene.search.SearcherManager; +import org.apache.lucene.store.MMapDirectory; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + + +@Disabled("prototype implementation") +public class OSMIndexTest { + + private static Path directory; + private static IndexSearcher searcher; + + @BeforeAll + public static void beforeAll() throws Exception { + // Init the geocoder service + directory = Files.createTempDirectory(Paths.get("."), "geocoder_"); + + // Create the geonames index + + var task = new CreateGeocoderOpenStreetMap(LIECHTENSTEIN_OSM_PBF, directory); + task.execute(new WorkflowContext()); + var dir = MMapDirectory.open(directory); + var searcherManager = new SearcherManager(dir, new SearcherFactory()); + searcher = searcherManager.acquire(); + } + + @AfterAll + public static void afterAll() throws IOException { + FileUtils.deleteRecursively(directory); + } + + @Test + void testCreateIndex() throws Exception { + var query = + new GeocoderOSMQuery("vaduz").build(); + var topDocs = searcher.search(query, 1); + var doc = searcher.doc(Arrays.stream(topDocs.scoreDocs).findFirst().get().doc); + assertEquals("Vaduz", doc.getField("name").stringValue()); + System.out.println(doc); + } + + + /** + * Querying document which contains a point with lat/long + */ + @Test + void testGeoQuery() throws Exception { + var vaduzLatLong = new double[] {47.1392862, 9.5227962}; + var query = LatLonShape.newPointQuery("polygon", ShapeField.QueryRelation.CONTAINS, + vaduzLatLong); + var topDocs = searcher.search(query, 10); + List<Document> docs = new ArrayList<>(); + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { + docs.add(searcher.doc(scoreDoc.doc)); + } + // Vaduz OSM relation:1155956 is present in results + // https://www.openstreetmap.org/relation/1155956 + var vaduz = docs.stream() + .filter(doc -> Long.parseLong(doc.getField("osm_id").stringValue()) == 1155956).findFirst(); Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/803) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
