Skip to content

Commit

Permalink
fix: Commented _globals and utils, removed unuseful tests and updated…
Browse files Browse the repository at this point in the history
… README
  • Loading branch information
JCruiz15 authored and jfaldanam committed Jun 25, 2024
1 parent 4c5264e commit 009bc1a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 146 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [SIGPAC-Tools](https://github.com/KhaosResearch/sigpac-tools)

![Python >=3.10](https://img.shields.io/badge/python-%3E=3.10-blue.svg)
![Python Version from PEP 621 TOML](https://img.shields.io/python/required-version-toml?tomlFilePath=https%3A%2F%2Fraw.githubusercontent.com%2FKhaosResearch%2Fsigpac-tools%2Fmain%2Fpyproject.toml)
![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)
![Status: Active](https://img.shields.io/badge/Status-Active-00aa00.svg)
![Code style: Ruff](https://img.shields.io/badge/code%20style-Ruff-aa0000.svg)
Expand Down
42 changes: 23 additions & 19 deletions sigpac_tools/_globals.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# URL from SIGPAC service
BASE_URL = "https://sigpac.mapama.gob.es"


# Provinces divided into communities
# https://www.ine.es/daco/daco42/codmun/cod_ccaa_provincia.htm
PROVINCES_BY_COMMUNITY = {
1: [4, 11, 14, 18, 21, 23, 29, 41],
2: [22, 44, 50],
3: [33],
4: [7],
5: [35, 38],
6: [39],
7: [5, 9, 24, 34, 37, 40, 42, 47, 49],
8: [2, 13, 16, 19, 45],
9: [8, 17, 25, 43],
10: [3, 12, 46],
11: [6, 10],
12: [15, 27, 32, 36],
13: [28],
14: [30],
15: [31],
16: [1, 20, 48],
17: [26],
18: [51],
19: [52],
1: [4, 11, 14, 18, 21, 23, 29, 41], # Andalucía : [Almería, Cádiz, Córdoba, Granada, Huelva, Jaén, Málaga, Sevilla]
2: [22, 44, 50], # Aragón : [Huesca, Teruel, Zaragoza]
3: [33], # Principado de Asturias : [Asturias]
4: [7], # Illes Balears : [Illes Balears]
5: [35, 38], # Canarias : [Las Palmas, Santa Cruz de Tenerife]
6: [39], # Cantabria : [Cantabria]
7: [5, 9, 24, 34, 37, 40, 42, 47, 49], # Castilla y León : [Ávila, Burgos, León, Palencia, Salamanca, Segovia, Soria, Valladolid, Zamora]
8: [2, 13, 16, 19, 45], # Castilla-La Mancha : [Albacete, Ciudad Real, Cuenca, Guadalajara, Toledo]
9: [8, 17, 25, 43], # Cataluña : [Barcelona, Girona, Lleida, Tarragona]
10: [3, 12, 46], # Comunitat Valenciana : [Alicante, Castellón, Valencia]
11: [6, 10], # Extremadura : [Badajoz, Cáceres]
12: [15, 27, 32, 36], # Galicia : [A Coruña, Lugo, Ourense, Pontevedra]
13: [28], # Comunidad de Madrid : [Madrid]
14: [30], # Región de Murcia : [Murcia]
15: [31], # Comunidad Foral de Navarra : [Navarra]
16: [1, 20, 48], # País Vasco : [Álava, Gipuzkoa, Bizkaia]
17: [26], # La Rioja : [La Rioja]
18: [51], # Ceuta : [Ceuta]
19: [52], # Melilla : [Melilla]
}
3 changes: 3 additions & 0 deletions sigpac_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def lng_lat_to_tile(lng: float, lat: float, zoom: float) -> tuple[int, int]:
tuple[int, int]
Returns a tuple with the x and y tile coordinates
"""

# Code adapted from https://github.com/DenisCarriere/global-mercator

ORIGIN_SHIFT = 2 * math.pi * 6378137 / 2.0
TILESIZE = 256

Expand Down
32 changes: 0 additions & 32 deletions tests/test_anotate.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import pytest
from unittest.mock import patch
from sigpac_tools.anotate import get_metadata

# Mock data for the test
layer_parcela_response = {"key": "value"}
layer_recinto_response = {"key": "value2"}


class TestGetMetadata:
@patch("sigpac_tools.anotate.__query")
def test_parcela_layer(self, mock_query):
mock_query.return_value = layer_parcela_response
data = {
"province": 1,
"municipality": 1,
"polygon": 1,
"parcel": 1,
}

result = get_metadata("parcela", data)
assert result == layer_parcela_response

@patch("sigpac_tools.anotate.__query")
def test_recinto_layer(self, mock_query):
mock_query.return_value = layer_recinto_response
data = {
"province": 1,
"municipality": 1,
"polygon": 1,
"parcel": 1,
"enclosure": 1,
}

result = get_metadata("recinto", data)
assert result == layer_recinto_response

def test_missing_layer(self):
data = {
"province": 1,
Expand Down
89 changes: 0 additions & 89 deletions tests/test_locate.py
Original file line number Diff line number Diff line change
@@ -1,99 +1,10 @@
import pytest
from unittest.mock import patch, Mock
from sigpac_tools.locate import geometry_from_coords
from sigpac_tools._globals import BASE_URL

# Mock data for the geojson response
mock_geojson_response = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"parcela": 123,
"recinto": 456,
},
"geometry": {
"type": "Polygon",
"coordinates": [[[0, 0], [1, 1], [1, 0], [0, 0]]],
},
}
],
}


class TestGeometryFromCoords:
@patch("sigpac_tools.locate.requests.get")
@patch("sigpac_tools.locate.lng_lat_to_tile")
@patch("sigpac_tools.locate.transform_coords")
def test_geometry_from_coords_parcela(
self, mock_transform_coords, mock_lng_lat_to_tile, mock_get
):
mock_lng_lat_to_tile.return_value = (1234, 5678)
mock_response = Mock()
mock_response.json.return_value = mock_geojson_response
mock_get.return_value = mock_response

layer = "parcela"
lat = 40.0
lon = -3.0
reference = 123

result = geometry_from_coords(layer, lat, lon, reference)

mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15)
mock_get.assert_called_once_with(
f"{BASE_URL}/vectorsdg/vector/parcela@3857/15.1234.5678.geojson"
)
mock_transform_coords.assert_called_once()
assert result == mock_geojson_response["features"][0]["geometry"]

@patch("sigpac_tools.locate.requests.get")
@patch("sigpac_tools.locate.lng_lat_to_tile")
@patch("sigpac_tools.locate.transform_coords")
def test_geometry_from_coords_recinto(
self, mock_transform_coords, mock_lng_lat_to_tile, mock_get
):
mock_lng_lat_to_tile.return_value = (1234, 5678)
mock_response = Mock()
mock_response.json.return_value = mock_geojson_response
mock_get.return_value = mock_response

layer = "recinto"
lat = 40.0
lon = -3.0
reference = 456

result = geometry_from_coords(layer, lat, lon, reference)

mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15)
mock_get.assert_called_once_with(
f"{BASE_URL}/vectorsdg/vector/recinto@3857/15.1234.5678.geojson"
)
mock_transform_coords.assert_called_once()
assert result == mock_geojson_response["features"][0]["geometry"]

@patch("sigpac_tools.locate.requests.get")
@patch("sigpac_tools.locate.lng_lat_to_tile")
def test_geometry_not_found(self, mock_lng_lat_to_tile, mock_get):
mock_lng_lat_to_tile.return_value = (1234, 5678)
mock_response = Mock()
mock_response.json.return_value = {"type": "FeatureCollection", "features": []}
mock_get.return_value = mock_response

layer = "parcela"
lat = 40.0
lon = -3.0
reference = 999

result = geometry_from_coords(layer, lat, lon, reference)

mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15)
mock_get.assert_called_once_with(
f"{BASE_URL}/vectorsdg/vector/parcela@3857/15.1234.5678.geojson"
)
assert result is None

def test_invalid_layer(self):
with pytest.raises(
KeyError,
Expand Down
5 changes: 0 additions & 5 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_search_provinces(self, mock_findCommunity, mock_get):
mock_get.assert_called_once_with(
f"{BASE_URL}/fega/ServiciosVisorSigpac/query/provincias/1.geojson"
)
assert result == provinces_response

@patch("sigpac_tools.search.requests.get")
@patch("sigpac_tools.search.findCommunity")
Expand All @@ -41,7 +40,6 @@ def test_search_municipalities(self, mock_findCommunity, mock_get):
mock_get.assert_called_once_with(
f"{BASE_URL}/fega/ServiciosVisorSigpac/query/municipios/1.geojson"
)
assert result == municipalities_response

@patch("sigpac_tools.search.requests.get")
def test_search_polygons(self, mock_get):
Expand All @@ -55,7 +53,6 @@ def test_search_polygons(self, mock_get):
mock_get.assert_called_once_with(
f"{BASE_URL}/fega/ServiciosVisorSigpac/query/poligonos/1/1/0/0.geojson"
)
assert result == polygons_response

@patch("sigpac_tools.search.requests.get")
def test_search_parcels(self, mock_get):
Expand All @@ -69,7 +66,6 @@ def test_search_parcels(self, mock_get):
mock_get.assert_called_once_with(
f"{BASE_URL}/fega/ServiciosVisorSigpac/query/parcelas/1/1/0/0/1.geojson"
)
assert result == parcels_response

@patch("sigpac_tools.search.requests.get")
def test_search_specific_parcel(self, mock_get):
Expand All @@ -83,7 +79,6 @@ def test_search_specific_parcel(self, mock_get):
mock_get.assert_called_once_with(
f"{BASE_URL}/fega/ServiciosVisorSigpac/query/recintos/1/1/0/0/1/1.geojson"
)
assert result == parcel_response

@patch("sigpac_tools.search.findCommunity")
def test_missing_community_and_province(self, mock_findCommunity):
Expand Down

0 comments on commit 009bc1a

Please sign in to comment.