Skip to content

Commit

Permalink
(gisService) add get farm info from layer service + unit test
Browse files Browse the repository at this point in the history
.

.
  • Loading branch information
jpn-geo6 committed Apr 12, 2024
1 parent 0c7f6df commit d00a3b4
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
12 changes: 12 additions & 0 deletions geosyspy/geosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,3 +1190,15 @@ def get_zarc_analytics(
seasonfield_id=sf_unique_id,
)
return self.check_status_and_metrics(task_id, "ZARC", sf_unique_id)

def get_farm_info_from_location(self, latitude:str, longitude:str):
"""get farm info from CAR layer
Args:
latitude (str): latitude of the location
longitude (str): longitude of the location
"""

return self.__gis_service.get_farm_info_from_location(latitude, longitude)

49 changes: 32 additions & 17 deletions geosyspy/services/gis_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" gis service class"""

from urllib.parse import urljoin
import logging
from geosyspy.utils.http_client import HttpClient
Expand All @@ -11,22 +12,17 @@ def __init__(self, base_url: str, http_client: HttpClient):
self.http_client: HttpClient = http_client
self.logger = logging.getLogger(__name__)

def get_municipio_id_from_geometry(self,
geometry:str
):
def get_municipio_id_from_geometry(self, geometry: str):
"""
method to call Gis Api to retrieve Municipio id from a geometry
method to call Gis Api to retrieve Municipio id from a geometry
Args:
geometry (str): the geometry (WKT or GeosJson) to retrieve the municipio Id
Returns:
the internal id of the municipio
Args:
geometry (str): the geometry (WKT or GeosJson) to retrieve the municipio Id
Returns:
the internal id of the municipio
"""

payload = {
"properties": ["id"],
"features": [geometry]
}
payload = {"properties": ["id"], "features": [geometry]}
parameters: str = "/layerservices/api/v1/layers/BRAZIL_MUNICIPIOS/intersect"
gis_url: str = urljoin(self.base_url, parameters)
response = self.http_client.post(gis_url, payload)
Expand All @@ -37,15 +33,34 @@ def get_municipio_id_from_geometry(self,
# extract & return municipio id from response
municipio_id = dict_response[0][0]["properties"]["id"]
if isinstance(municipio_id, int):
return municipio_id
return municipio_id
self.logger.warning("No municipio id found for this geometry")
return 0
except Exception:
return 0

else:
self.logger.info(response.status_code)
raise ValueError(
"No municipio id found for this geometry"
)

raise ValueError("No municipio id found for this geometry")

def get_farm_info_from_location(self, latitude: str, longitude: str):
"""
method to call Gis Api to retrieve Municipio id from a geometry
Args:
latitude (str): the latitude of the location
longitude (str): the longitude of the location
Returns:
the farm boundary & informations
"""

parameters: str = (
f"/layerservices/api/v1/layers/BR_CAR_PROPERTIES/feature?LOCATION={latitude},{longitude}&format=wkt"
)
gis_url: str = urljoin(self.base_url, parameters)
response = self.http_client.get(gis_url)
if response.status_code == 200:
return response.json()

self.logger.error(response.status_code)
raise ValueError(f"No farm found for the location ({latitude}, {longitude}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"geometry": "MULTIPOLYGON (((-50.7550046 -15.0103631, -50.7522972 -15.023794, -50.7538391 -15.0465947, -50.7545579 -15.046742, -50.754773 -15.0469884, -50.7547436 -15.0482217, -50.7540695 -15.0499172, -50.7544351 -15.0501764, -50.756328 -15.0500917, -50.7571211 -15.0494465, -50.7576034 -15.0473754, -50.7570527 -15.0453142, -50.7563015 -15.0441924, -50.7567879 -15.0434991, -50.7575225 -15.0436098, -50.7585857 -15.0447496, -50.7596043 -15.0452414, -50.7603649 -15.0449181, -50.7616553 -15.0440355, -50.7616121 -15.0431305, -50.7613723 -15.0418976, -50.7617401 -15.0416567, -50.762959 -15.0419004, -50.7640369 -15.042568, -50.7646193 -15.0429295, -50.7650567 -15.0431845, -50.7658407 -15.0434939, -50.766751 -15.043111, -50.7672908 -15.0431468, -50.7677682 -15.0429368, -50.7685547 -15.0426848, -50.7697732 -15.0406726, -50.7680222 -15.0406117, -50.7672749 -15.0405345, -50.7651876 -15.0393137, -50.7648563 -15.0382981, -50.7650145 -15.0364739, -50.7653862 -15.0362524, -50.7693035 -15.035722, -50.7697737 -15.0358889, -50.7709647 -15.0373586, -50.7708193 -15.0386097, -50.7709373 -15.0390197, -50.7720428 -15.0396032, -50.7725891 -15.0384564, -50.7729165 -15.0383043, -50.7742116 -15.0384986, -50.7747909 -15.038244, -50.7758195 -15.0372373, -50.7762632 -15.03623, -50.7780799 -15.0330885, -50.7781857 -15.0325468, -50.7780654 -15.0320636, -50.777757 -15.0316419, -50.7769536 -15.0312092, -50.7757138 -15.0308744, -50.7747518 -15.0309563, -50.7745774 -15.0308982, -50.7740516 -15.0305182, -50.7734767 -15.0298815, -50.7735701 -15.0296531, -50.7738982 -15.0286581, -50.774184 -15.0284016, -50.7748328 -15.0276207, -50.7754312 -15.0275145, -50.7757102 -15.0276328, -50.7796548 -15.0258723, -50.780127 -15.0262423, -50.7811347 -15.0272762, -50.781305 -15.0277188, -50.7817941 -15.0281878, -50.7820779 -15.0282239, -50.7823196 -15.0281443, -50.7834333 -15.0267857, -50.7829228 -15.0257474, -50.7831994 -15.0254985, -50.7835089 -15.0252633, -50.7841565 -15.0250814, -50.7848911 -15.0243791, -50.7853404 -15.0241867, -50.7870566 -15.0238922, -50.7870912 -15.0236306, -50.7862734 -15.0226654, -50.7861974 -15.0221098, -50.7866939 -15.0217266, -50.7906038 -15.0214404, -50.7907973 -15.0229788, -50.7906563 -15.0232049, -50.7890399 -15.0237091, -50.7888099 -15.0241128, -50.7889344 -15.0242576, -50.7898846 -15.0242132, -50.7905852 -15.0241458, -50.7912689 -15.0245578, -50.7920637 -15.0256451, -50.7924826 -15.0257035, -50.7931946 -15.0254047, -50.7933811 -15.0230902, -50.7938141 -15.0226801, -50.794151 -15.0226207, -50.7948298 -15.0227879, -50.7954053 -15.0232526, -50.7958325 -15.0239641, -50.797318 -15.0248672, -50.7985037 -15.0226149, -50.7982396 -15.02118, -50.7983311 -15.0204272, -50.7987763 -15.0179476, -50.7989953 -15.0172728, -50.7996894 -15.01626, -50.7997046 -15.0156527, -50.7983846 -15.0152754, -50.7980119 -15.0148455, -50.7979604 -15.014604, -50.7981278 -15.0142519, -50.7983665 -15.0140763, -50.7989282 -15.0140124, -50.8007522 -15.0151386, -50.8014132 -15.0156664, -50.8018339 -15.0169669, -50.8025832 -15.017275, -50.8037138 -15.0174594, -50.8042834 -15.0172799, -50.8045514 -15.0167627, -50.8041927 -15.015284, -50.8041368 -15.0143489, -50.8048149 -15.0137934, -50.8056305 -15.0135434, -50.8068956 -15.0138675, -50.807805 -15.0131489, -50.8083866 -15.0122682, -50.809121 -15.011626, -50.8098691 -15.0111778, -50.8104878 -15.0107593, -50.8107281 -15.0105791, -50.8112246 -15.0102672, -50.8116077 -15.009961, -50.8120636 -15.0095743, -50.8125667 -15.0093342, -50.8125909 -15.008247, -50.8128326 -15.0072348, -50.8128522 -15.0063934, -50.8129774 -15.0059261, -50.8130501 -15.0055965, -50.8133043 -15.0047848, -50.8133919 -15.0042525, -50.8133941 -15.0035195, -50.8133182 -15.0025953, -50.8132604 -15.0019216, -50.8132863 -15.0014659, -50.813874 -15.0008804, -50.8147547 -15.0009941, -50.8152175 -15.0011199, -50.8155598 -15.001285, -50.8159891 -15.0014387, -50.8164243 -15.0014113, -50.8169903 -15.0012173, -50.8173626 -15.0002081, -50.7618552 -14.9892969, -50.757984 -15.0078372, -50.7550046 -15.0103631)))",
"properties": {
"IDF": "",
"TEMA": "AREA_IMOVEL_merged",
"NUM_AREA": 2334.56494965217,
"COD_ESTADO": "52",
"NOM_ESTADO": "Goiás",
"COD_MUNICIPIO": "5202155",
"NOM_MUNICIPIO": "Araguapaz",
"COD_IMOVEL": "GO-5202155-240BE44E436743D694B8FF0134F19952",
"CONDICAO_I": "Aguardando análise",
"NOM_MUNICI": "",
"NUM_MODULO": "51.8521",
"SITUACAO": "AT",
"TIPO_IMOVE": "IRU"
}
},
{
"geometry": "MULTIPOLYGON (((-50.7550046 -15.0103631, -50.7522972 -15.023794, -50.7538391 -15.0465947, -50.7545579 -15.046742, -50.754773 -15.0469884, -50.7547436 -15.0482217, -50.7540695 -15.0499172, -50.7544351 -15.0501764, -50.756328 -15.0500917, -50.7571211 -15.0494465, -50.7576034 -15.0473754, -50.7570527 -15.0453142, -50.7563015 -15.0441924, -50.7567879 -15.0434991, -50.7575225 -15.0436098, -50.7585857 -15.0447496, -50.7596043 -15.0452414, -50.7603649 -15.0449181, -50.7616553 -15.0440355, -50.7616121 -15.0431305, -50.7613723 -15.0418976, -50.7617401 -15.0416567, -50.762959 -15.0419004, -50.7640369 -15.042568, -50.7646193 -15.0429295, -50.7650567 -15.0431845, -50.7658407 -15.0434939, -50.766751 -15.043111, -50.7672908 -15.0431468, -50.7677682 -15.0429368, -50.7685547 -15.0426848, -50.7697732 -15.0406726, -50.7680222 -15.0406117, -50.7672749 -15.0405345, -50.7651876 -15.0393137, -50.7648563 -15.0382981, -50.7650145 -15.0364739, -50.7653862 -15.0362524, -50.7693035 -15.035722, -50.7697737 -15.0358889, -50.7709647 -15.0373586, -50.7708193 -15.0386097, -50.7709373 -15.0390197, -50.7720428 -15.0396032, -50.7725891 -15.0384564, -50.7729165 -15.0383043, -50.7742116 -15.0384986, -50.7747909 -15.038244, -50.7758195 -15.0372373, -50.7762632 -15.03623, -50.7780799 -15.0330885, -50.7781857 -15.0325468, -50.7780654 -15.0320636, -50.777757 -15.0316419, -50.7769536 -15.0312092, -50.7757138 -15.0308744, -50.7747518 -15.0309563, -50.7745774 -15.0308982, -50.7740516 -15.0305182, -50.7734767 -15.0298815, -50.7735701 -15.0296531, -50.7738982 -15.0286581, -50.774184 -15.0284016, -50.7748328 -15.0276207, -50.7754312 -15.0275145, -50.7757102 -15.0276328, -50.7796548 -15.0258723, -50.780127 -15.0262423, -50.7811347 -15.0272762, -50.781305 -15.0277188, -50.7817941 -15.0281878, -50.7820779 -15.0282239, -50.7823196 -15.0281443, -50.7834333 -15.0267857, -50.7829228 -15.0257474, -50.7831994 -15.0254985, -50.7835089 -15.0252633, -50.7841565 -15.0250814, -50.7848911 -15.0243791, -50.7853404 -15.0241867, -50.7870566 -15.0238922, -50.7870912 -15.0236306, -50.7862734 -15.0226654, -50.7861974 -15.0221098, -50.7866939 -15.0217266, -50.7906038 -15.0214404, -50.7907973 -15.0229788, -50.7906563 -15.0232049, -50.7890399 -15.0237091, -50.7888099 -15.0241128, -50.7889344 -15.0242576, -50.7898846 -15.0242132, -50.7905852 -15.0241458, -50.7912689 -15.0245578, -50.7920637 -15.0256451, -50.7924826 -15.0257035, -50.7931946 -15.0254047, -50.7933811 -15.0230902, -50.7938141 -15.0226801, -50.794151 -15.0226207, -50.7948298 -15.0227879, -50.7954053 -15.0232526, -50.7958325 -15.0239641, -50.797318 -15.0248672, -50.7985037 -15.0226149, -50.7982396 -15.02118, -50.7983311 -15.0204272, -50.7987763 -15.0179476, -50.7989953 -15.0172728, -50.7996894 -15.01626, -50.7997046 -15.0156527, -50.7983846 -15.0152754, -50.7980119 -15.0148455, -50.7979604 -15.014604, -50.7981278 -15.0142519, -50.7983665 -15.0140763, -50.7989282 -15.0140124, -50.8007522 -15.0151386, -50.8014132 -15.0156664, -50.8018339 -15.0169669, -50.8025832 -15.017275, -50.8037138 -15.0174594, -50.8042834 -15.0172799, -50.8045514 -15.0167627, -50.8041927 -15.015284, -50.8041368 -15.0143489, -50.8048149 -15.0137934, -50.8056305 -15.0135434, -50.8068956 -15.0138675, -50.807805 -15.0131489, -50.8083866 -15.0122682, -50.809121 -15.011626, -50.8098691 -15.0111778, -50.8104878 -15.0107593, -50.8107281 -15.0105791, -50.8112246 -15.0102672, -50.8116077 -15.009961, -50.8120636 -15.0095743, -50.8125667 -15.0093342, -50.8125909 -15.008247, -50.8128326 -15.0072348, -50.8128522 -15.0063934, -50.8129774 -15.0059261, -50.8130501 -15.0055965, -50.8133043 -15.0047848, -50.8133919 -15.0042525, -50.8133941 -15.0035195, -50.8133182 -15.0025953, -50.8132604 -15.0019216, -50.8132863 -15.0014659, -50.813874 -15.0008804, -50.8147547 -15.0009941, -50.8152175 -15.0011199, -50.8155598 -15.001285, -50.8159891 -15.0014387, -50.8164243 -15.0014113, -50.8169903 -15.0012173, -50.8173626 -15.0002081, -50.7618552 -14.9892969, -50.757984 -15.0078372, -50.7550046 -15.0103631)))",
"properties": {
"IDF": "",
"TEMA": "AREA_IMOVEL_merged",
"NUM_AREA": 2334.56494965217,
"COD_ESTADO": "52",
"NOM_ESTADO": "Goiás",
"COD_MUNICIPIO": "5202155",
"NOM_MUNICIPIO": "Araguapaz",
"COD_IMOVEL": "GO-5202155-240BE44E436743D694B8FF0134F19952",
"CONDICAO_I": "Aguardando análise",
"NOM_MUNICI": "",
"NUM_MODULO": "51.8521",
"SITUACAO": "AT",
"TIPO_IMOVE": "IRU"
}
}
]
29 changes: 17 additions & 12 deletions tests/test_unit_gis_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
from datetime import datetime
from unittest.mock import patch
from geosyspy import Geosys
from dotenv import load_dotenv
import datetime as dt
import numpy as np

from geosyspy.services.gis_service import GisService
from geosyspy.utils.constants import *
from geosyspy.utils.http_client import *
from tests.test_helper import *
from geosyspy.utils.http_client import HttpClient
from tests.test_helper import mock_http_response_text_content, load_data_from_textfile

geometry = "POLYGON((-91.17523978603823 40.29787117039518,-91.17577285022956 40.29199489606421,-91.167613719932 40.29199489606421,-91.1673028670095 40.29867040193312,-91.17523978603823 40.29787117039518))"
GEOMETRY = "POLYGON((-91.17523978603823 40.29787117039518,-91.17577285022956 40.29199489606421,-91.167613719932 40.29199489606421,-91.1673028670095 40.29867040193312,-91.17523978603823 40.29787117039518))"
LATITUDE = -15.01402
LONGITUDE = -50.7717


class TestGisService:
Expand All @@ -28,5 +23,15 @@ def test_get_municipio_id_from_geometry(self, post_response):
post_response.return_value = mock_http_response_text_content("POST", load_data_from_textfile(
"gis_layer_municipio_data_mock_http_response"))

municipio_id = self.service.get_municipio_id_from_geometry(geometry=geometry)
assert municipio_id == 121935
municipio_id = self.service.get_municipio_id_from_geometry(geometry=GEOMETRY)
assert municipio_id == 121935


@patch('geosyspy.utils.http_client.HttpClient.get')
def test_get_farm_info_from_location(self, get_response):
get_response.return_value = mock_http_response_text_content("GET", load_data_from_textfile(
"get_farm_info_from_location_data_mock_http_response"))

result = self.service.get_farm_info_from_location(latitude=LATITUDE, longitude=LONGITUDE)
assert result is not None
assert result[0]["properties"]["NOM_MUNICIPIO"] == "Araguapaz"

0 comments on commit d00a3b4

Please sign in to comment.