github-advanced-security[bot] commented on code in PR #569: URL: https://github.com/apache/incubator-baremaps/pull/569#discussion_r1405884773
########## baremaps-server/src/main/java/org/apache/baremaps/server/TdTilesResources.java: ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.server; + +import static com.google.common.net.HttpHeaders.*; + +import de.javagl.jgltf.model.NodeModel; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.sql.DataSource; +import javax.ws.rs.GET; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; +import org.apache.baremaps.tdtiles.GltfBuilder; +import org.apache.baremaps.tdtiles.TdTilesStore; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tdtiles.subtree.Availability; +import org.apache.baremaps.tdtiles.subtree.Subtree; + +@Singleton [email protected]("/") +public class TdTilesResources { + private final TdTilesStore tdTilesStore; + + @Inject + public TdTilesResources(DataSource dataSource) { + this.tdTilesStore = new TdTilesStore(dataSource); + } + + @GET + @javax.ws.rs.Path("/subtrees/{level}.{x}.{y}.json") + public Response getSubtree(@PathParam("level") int level, @PathParam("x") int x, Review Comment: ## Useless parameter The parameter 'x' is never used. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/849) ########## baremaps-server/src/main/java/org/apache/baremaps/server/TdTilesResources.java: ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.server; + +import static com.google.common.net.HttpHeaders.*; + +import de.javagl.jgltf.model.NodeModel; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.sql.DataSource; +import javax.ws.rs.GET; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; +import org.apache.baremaps.tdtiles.GltfBuilder; +import org.apache.baremaps.tdtiles.TdTilesStore; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tdtiles.subtree.Availability; +import org.apache.baremaps.tdtiles.subtree.Subtree; + +@Singleton [email protected]("/") +public class TdTilesResources { + private final TdTilesStore tdTilesStore; + + @Inject + public TdTilesResources(DataSource dataSource) { + this.tdTilesStore = new TdTilesStore(dataSource); + } + + @GET + @javax.ws.rs.Path("/subtrees/{level}.{x}.{y}.json") + public Response getSubtree(@PathParam("level") int level, @PathParam("x") int x, + @PathParam("y") int y) { + if (level == 18) { + return Response.ok() + .entity( + new Subtree(new Availability(false), new Availability(true), new Availability(false))) + .header(CONTENT_TYPE, "application/json").build(); + } + return Response.ok() + .entity(new Subtree(new Availability(true), new Availability(true), new Availability(true))) + .header(CONTENT_TYPE, "application/json").build(); + } + + @GET + @javax.ws.rs.Path("/content/content_{level}__{x}_{y}.glb") + public Response getContent(@PathParam("level") int level, @PathParam("x") int x, + @PathParam("y") int y) throws Exception { + if (level < 14) { + return Response.ok().entity( + GltfBuilder.createGltf(new ArrayList<>())).build(); + } + float[] coords = xyzToLatLonRadians(x, y, level); + List<NodeModel> nodes = new ArrayList<>(); + int limit = level > 17 ? 1000 : level > 16 ? 200 : level > 15 ? 30 : 10; + List<Building> buildings = tdTilesStore.read(coords[0], coords[1], coords[2], coords[3], limit); + for (Building building : buildings) { + float tolerance = level > 17 ? 0.00001f : level > 15 ? 0.00002f : 0.00004f; + nodes.add(GltfBuilder.createNode(building, tolerance)); + } + return Response.ok().entity( + GltfBuilder.createGltf(nodes)).build(); + } + + @GET + @javax.ws.rs.Path("/{path:.*}") + public Response get(@PathParam("path") String path) { + if (path.equals("") || path.endsWith("/")) { Review Comment: ## Inefficient empty string test Inefficient comparison to empty string, check for zero length instead. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/845) ########## baremaps-server/src/main/java/org/apache/baremaps/server/TdTilesResources.java: ########## @@ -0,0 +1,124 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.server; + +import static com.google.common.net.HttpHeaders.*; + +import de.javagl.jgltf.model.NodeModel; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.sql.DataSource; +import javax.ws.rs.GET; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; +import org.apache.baremaps.tdtiles.GltfBuilder; +import org.apache.baremaps.tdtiles.TdTilesStore; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tdtiles.subtree.Availability; +import org.apache.baremaps.tdtiles.subtree.Subtree; + +@Singleton [email protected]("/") +public class TdTilesResources { + private final TdTilesStore tdTilesStore; + + @Inject + public TdTilesResources(DataSource dataSource) { + this.tdTilesStore = new TdTilesStore(dataSource); + } + + @GET + @javax.ws.rs.Path("/subtrees/{level}.{x}.{y}.json") + public Response getSubtree(@PathParam("level") int level, @PathParam("x") int x, + @PathParam("y") int y) { Review Comment: ## Useless parameter The parameter 'y' is never used. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/850) ########## baremaps-core/src/main/java/org/apache/baremaps/tdtiles/TdTilesStore.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.tdtiles; + + + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.*; +import javax.sql.DataSource; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tilestore.TileStoreException; +import org.apache.baremaps.utils.GeometryUtils; +import org.locationtech.jts.geom.Geometry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A read-only {@code TileStore} implementation that uses the PostgreSQL to generate 3d tiles. + */ +public class TdTilesStore { + + private static final Logger logger = LoggerFactory.getLogger(TdTilesStore.class); + private static final String QUERY = + "select st_asbinary(geom), tags -> 'buildings:height', tags -> 'height', tags -> 'buildings:levels' from osm_ways where tags ? 'building' and st_intersects(geom, st_makeenvelope(%1$s, %2$s, %3$s, %4$s, 4326)) LIMIT %5$s"; + + + private final DataSource datasource; + + public TdTilesStore(DataSource datasource) { + this.datasource = datasource; + } + + public List<Building> read(float xmin, float xmax, float ymin, float ymax, int limit) + throws TileStoreException { + try (Connection connection = datasource.getConnection(); + Statement statement = connection.createStatement()) { + + String sql = String.format(QUERY, ymin * 180 / (float) Math.PI, xmin * 180 / (float) Math.PI, + ymax * 180 / (float) Math.PI, xmax * 180 / (float) Math.PI, limit); + + logger.debug("Executing query: {}", sql); + System.out.println(sql); + + List<Building> buildings = new ArrayList<>(); + + + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + byte[] bytes = resultSet.getBytes(1); + Geometry geometry = GeometryUtils.deserialize(bytes); + + String buildingHeight = resultSet.getString(2); + String height = resultSet.getString(3); + String buildingLevels = resultSet.getString(4); + float finalHeight = 10; + if (buildingHeight != null) { + finalHeight = Float.parseFloat(buildingHeight.replaceAll("[^0-9]", "")); + } else if (height != null) { + finalHeight = Float.parseFloat(height.replaceAll("[^0-9]", "")); Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/847) ########## baremaps-core/src/main/java/org/apache/baremaps/tdtiles/TdTilesStore.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.tdtiles; + + + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.*; +import javax.sql.DataSource; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tilestore.TileStoreException; +import org.apache.baremaps.utils.GeometryUtils; +import org.locationtech.jts.geom.Geometry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A read-only {@code TileStore} implementation that uses the PostgreSQL to generate 3d tiles. + */ +public class TdTilesStore { + + private static final Logger logger = LoggerFactory.getLogger(TdTilesStore.class); + private static final String QUERY = + "select st_asbinary(geom), tags -> 'buildings:height', tags -> 'height', tags -> 'buildings:levels' from osm_ways where tags ? 'building' and st_intersects(geom, st_makeenvelope(%1$s, %2$s, %3$s, %4$s, 4326)) LIMIT %5$s"; + + + private final DataSource datasource; + + public TdTilesStore(DataSource datasource) { + this.datasource = datasource; + } + + public List<Building> read(float xmin, float xmax, float ymin, float ymax, int limit) + throws TileStoreException { + try (Connection connection = datasource.getConnection(); + Statement statement = connection.createStatement()) { + + String sql = String.format(QUERY, ymin * 180 / (float) Math.PI, xmin * 180 / (float) Math.PI, + ymax * 180 / (float) Math.PI, xmax * 180 / (float) Math.PI, limit); + + logger.debug("Executing query: {}", sql); + System.out.println(sql); + + List<Building> buildings = new ArrayList<>(); + + + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + byte[] bytes = resultSet.getBytes(1); + Geometry geometry = GeometryUtils.deserialize(bytes); + + String buildingHeight = resultSet.getString(2); + String height = resultSet.getString(3); + String buildingLevels = resultSet.getString(4); + float finalHeight = 10; + if (buildingHeight != null) { + finalHeight = Float.parseFloat(buildingHeight.replaceAll("[^0-9]", "")); + } else if (height != null) { + finalHeight = Float.parseFloat(height.replaceAll("[^0-9]", "")); + } else if (buildingLevels != null) { + finalHeight = Float.parseFloat(buildingLevels.replaceAll("[^0-9]", "")) * 3; Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/848) ########## baremaps-core/src/main/java/org/apache/baremaps/tdtiles/TdTilesStore.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you 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.tdtiles; + + + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.*; +import javax.sql.DataSource; +import org.apache.baremaps.tdtiles.building.Building; +import org.apache.baremaps.tilestore.TileStoreException; +import org.apache.baremaps.utils.GeometryUtils; +import org.locationtech.jts.geom.Geometry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A read-only {@code TileStore} implementation that uses the PostgreSQL to generate 3d tiles. + */ +public class TdTilesStore { + + private static final Logger logger = LoggerFactory.getLogger(TdTilesStore.class); + private static final String QUERY = + "select st_asbinary(geom), tags -> 'buildings:height', tags -> 'height', tags -> 'buildings:levels' from osm_ways where tags ? 'building' and st_intersects(geom, st_makeenvelope(%1$s, %2$s, %3$s, %4$s, 4326)) LIMIT %5$s"; + + + private final DataSource datasource; + + public TdTilesStore(DataSource datasource) { + this.datasource = datasource; + } + + public List<Building> read(float xmin, float xmax, float ymin, float ymax, int limit) + throws TileStoreException { + try (Connection connection = datasource.getConnection(); + Statement statement = connection.createStatement()) { + + String sql = String.format(QUERY, ymin * 180 / (float) Math.PI, xmin * 180 / (float) Math.PI, + ymax * 180 / (float) Math.PI, xmax * 180 / (float) Math.PI, limit); + + logger.debug("Executing query: {}", sql); + System.out.println(sql); + + List<Building> buildings = new ArrayList<>(); + + + try (ResultSet resultSet = statement.executeQuery(sql)) { + while (resultSet.next()) { + byte[] bytes = resultSet.getBytes(1); + Geometry geometry = GeometryUtils.deserialize(bytes); + + String buildingHeight = resultSet.getString(2); + String height = resultSet.getString(3); + String buildingLevels = resultSet.getString(4); + float finalHeight = 10; + if (buildingHeight != null) { + finalHeight = Float.parseFloat(buildingHeight.replaceAll("[^0-9]", "")); Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/846) -- 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]
