Skip to content

Commit

Permalink
Bug correction with the coordinate reference systems
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesGrillot committed May 16, 2024
1 parent b36c60a commit 9df52eb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 26 deletions.
2 changes: 2 additions & 0 deletions bd_topo_extractor/__about__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ def plugin_metadata_as_dict() -> dict:
__wfs_name__: str = __plugin_md__.get("wfs").get("name")
__wfs_uri__: str = __plugin_md__.get("wfs").get("uri")
__wfs_schema__: str = __plugin_md__.get("wfs").get("schema")
__wfs_crs__: str = __plugin_md__.get("wfs").get("crs")
__wfs_geometry__: str = __plugin_md__.get("wfs").get("geometry")
__wfs_metadata__: str = __plugin_md__.get("wfs").get("metadata")
__wfs_credit__: str = __plugin_md__.get("wfs").get("producer")
__wfs_logo__: Path = DIR_PLUGIN_ROOT.resolve() / __plugin_md__.get("wfs").get("logo")
Expand Down
21 changes: 10 additions & 11 deletions bd_topo_extractor/gui/dlg_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
__wfs_logo__,
__wfs_metadata__,
__wfs_credit__,
__wfs_crs__,
)
from bd_topo_extractor.processing import RectangleDrawTool
from bd_topo_extractor.processing import GetCapabilitiesRequest
Expand Down Expand Up @@ -371,8 +372,6 @@ def __init__(self, project=None, iface=None, url=None, manager=None):
self.draw_rectangle_button.setDisabled
)

# self.select_layer_combo_box.layerChanged.connect(self.check_layer_size)

self.select_layer_checkbox.stateChanged.connect(
self.button_box.setEnabled)
self.select_layer_checkbox.stateChanged.connect(self.erase_rubber_band)
Expand Down Expand Up @@ -415,7 +414,7 @@ def show_max_extent(self, value):
self.canvas.refresh()
else:
self.max_extent_layer = QgsVectorLayer(
"Polygon?crs=epsg:4326", "Max extent", "memory"
"Polygon?crs=epsg:" + str(__wfs_crs__), "Max extent", "memory"
)
self.max_extent_layer.startEditing()
new_geom = QgsGeometry().fromRect(
Expand Down Expand Up @@ -446,7 +445,7 @@ def check_layer_size(self):
layer = self.select_layer_combo_box.currentLayer()
# Reproject the layer
transformed_extent = self.transform_crs(
layer.extent(), layer.crs())
layer.extent(), layer.crs(), QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)))
if self.getcapabilities.max_bounding_box.intersects(transformed_extent): # noqa: E501
if transformed_extent.area() > 100000000:
msg = QMessageBox()
Expand Down Expand Up @@ -506,17 +505,17 @@ def get_result(self):
# Remove the map tool to draw the rectangle
self.canvas.unsetMapTool(self.rectangle_tool)
# Get the rectangle extent and reproject it
self.extent = self.transform_crs(
self.rectangle_tool.rectangle(),
QgsCoordinateReferenceSystem("EPSG:4326"),
)
self.extent = self.rectangle_tool.new_extent
# If the extent is from a layer
else:
# Get the layer
self.layer = self.select_layer_combo_box.currentLayer()
# Reproject the layer
self.extent = self.transform_crs(
self.layer.extent(), self.layer.crs())
self.layer.extent(),
self.layer.crs(),
QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)),
)

def signal_accept(self, msg):
# Update the progress bar when result is pressed
Expand Down Expand Up @@ -654,11 +653,11 @@ def check_rectangle(self):
elif self.draw_rectangle_checkbox.isChecked():
self.rectangle = None

def transform_crs(self, rectangle, input_crs):
def transform_crs(self, rectangle, input_crs, output_crs):
# Reproject a rectangle to the project crs
geom = QgsGeometry().fromRect(rectangle)
geom.transform(
QgsCoordinateTransform(input_crs, self.project.crs(), self.project)
QgsCoordinateTransform(input_crs, output_crs, self.project)
)
transformed_extent = geom.boundingBox()
return transformed_extent
Expand Down
2 changes: 1 addition & 1 deletion bd_topo_extractor/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ name=BD TOPO®
uri=https://data.geopf.fr/wfs/ows
schema=BDTOPO_V3
geometry=geometrie
crs=3857
crs=4326
metadata=https://geoservices.ign.fr/bdtopo
producer=https://www.ign.fr/
logo=resources/images/logo.png
4 changes: 1 addition & 3 deletions bd_topo_extractor/plugin_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ def handle_finished(self):
# If there is no layers, an OSM layer is added
# to simplify the rectangle drawing
if len(self.project.instance().mapLayers()) == 0:
self.project.instance().setCrs(
QgsCoordinateReferenceSystem("EPSG:3857")
)

# Type of WMTS, url and name
type = "xyz"
url = "http://tile.openstreetmap.org/{z}/{x}/{y}.png"
Expand Down
14 changes: 9 additions & 5 deletions bd_topo_extractor/processing/rectangle_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from PyQt5.QtGui import QColor
from PyQt5.QtCore import Qt, pyqtSignal

# project
from bd_topo_extractor.__about__ import (
__wfs_crs__,
)

class RectangleDrawTool(QgsMapTool):
signal = pyqtSignal()
Expand Down Expand Up @@ -94,19 +98,19 @@ def rectangle(self):
return None
else:
# Rectangle reprojection
if str(self.project.instance().crs().postgisSrid()) != "4326":
if str(self.project.instance().crs().postgisSrid()) != str(__wfs_crs__):
start_point = self.transform_geom(
QgsGeometry().fromPointXY(self.start_point),
self.project.instance().crs(),
QgsCoordinateReferenceSystem("EPSG:4326"),
QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)),
)
self.start_point = QgsPointXY(
start_point.asPoint().x(), start_point.asPoint().y()
)
end_point = self.transform_geom(
QgsGeometry().fromPointXY(self.end_point),
self.project.instance().crs(),
QgsCoordinateReferenceSystem("EPSG:4326"),
QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)),
)
self.end_point = QgsPointXY(
end_point.asPoint().x(), end_point.asPoint().y()
Expand All @@ -120,8 +124,8 @@ def rectangle(self):
QgsGeometry().fromRect(
QgsRectangle(self.start_point, self.end_point)
),
QgsCoordinateReferenceSystem("EPSG:4326"),
QgsCoordinateReferenceSystem("EPSG:3857"),
QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)),
self.project.crs(),
)
transformed_extent = drawned_rectangle.boundingBox()
# If the drawn rectangle is too big, an error message appear
Expand Down
18 changes: 12 additions & 6 deletions bd_topo_extractor/processing/wfs_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import processing
import os.path

# project
from bd_topo_extractor.__about__ import (
__wfs_geometry__,
__wfs_crs__,
)

class WfsRequest:
def __init__(
Expand Down Expand Up @@ -80,14 +85,15 @@ def build_url(self):
self.uri.setParam("typename", self.data)
self.uri.setParam("title", self.data)
self.uri.setParam("table", "")
self.uri.setParam("srsName", "EPSG:" + str("4326"))
sql = "SELECT * FROM \"{data}\" as t1 WHERE ST_Intersects(t1.{geometry_column}, ST_GeometryFromText('Polygon (({xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}))', 4326))".format(
self.uri.setParam("srsName", "EPSG:" + str(__wfs_crs__))
sql = "SELECT * FROM \"{data}\" as t1 WHERE ST_Intersects(t1.{geometry_column}, ST_GeometryFromText('Polygon (({xmin} {ymin}, {xmax} {ymin}, {xmax} {ymax}, {xmin} {ymax}, {xmin} {ymin}))', {crs}))".format(
data=self.data,
geometry_column="geometrie",
geometry_column=str(__wfs_geometry__),
ymin=str(self.boundingbox.yMinimum()),
xmin=str(self.boundingbox.xMinimum()),
ymax=str(self.boundingbox.yMaximum()),
xmax=str(self.boundingbox.xMaximum()),
crs=str(__wfs_crs__),
)
self.uri.setSql(sql)

Expand Down Expand Up @@ -126,7 +132,7 @@ def create_layer(self):
geom_type = "Linestring"
# Create a memory layer
new_layer = QgsVectorLayer(
geom_type + "?crs=epsg:4326",
geom_type + "?crs=epsg:" + str(__wfs_crs__),
str(self.export_name),
"memory",
)
Expand All @@ -144,7 +150,7 @@ def create_layer(self):
if self.geom == "within":
# Creation of a layer with the extent.
clipping_layer = QgsVectorLayer(
"Polygon?crs=epsg:4326", "clipper", "memory"
"Polygon?crs=epsg:" + str(__wfs_crs__), "clipper", "memory"
)
clipping_layer.startEditing()
new_geom = QgsGeometry().fromRect(self.boundingbox)
Expand All @@ -170,7 +176,7 @@ def create_layer(self):
driver = "GPKG"
context = self.project.instance().transformContext()
options = QgsVectorFileWriter.SaveVectorOptions()
tr = QgsCoordinateTransform(QgsCoordinateReferenceSystem(4326), self.crs, self.project.instance())
tr = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:" + str(__wfs_crs__)), self.crs, self.project.instance())
options.ct = tr
# Check if the GeoPackage already exists,
# to know if it's need to be created or not
Expand Down

0 comments on commit 9df52eb

Please sign in to comment.