* adds try-catch if WFS result is not a valid json
This commit is contained in:
mpeltriaux 2022-08-05 11:31:52 +02:00
parent 7acd11096b
commit a8ffcf856d
2 changed files with 29 additions and 8 deletions

View File

@ -132,9 +132,9 @@ class Geometry(BaseResource):
lock_id = f"parcel_calc-lock-{flr_id}"
with cache_lock(lock_id) as acquired:
while not acquired:
print(f"Am locked. Need to rest. Calculating: {parcel_properties}")
sleep(0.5)
acquired = cache_lock(lock_id)
district = District.objects.get_or_create(
key=parcel_properties["kreisschl"],
name=parcel_properties["kreis"],

View File

@ -86,13 +86,14 @@ class ParcelWFSFetcher(AbstractWFSFetcher):
from konova.models import Geometry
if filter_srid is None:
filter_srid = DEFAULT_SRID_RLP
geom_gml = Geometry.objects.filter(
geom = Geometry.objects.filter(
id=self.geometry_id
).annotate(
transformed=Transform(srid=filter_srid, expression="geom")
).annotate(
gml=AsGML(MakeValid('transformed'))
).first().gml
).first()
geom_gml = geom.gml
spatial_filter = f"<Filter><{geometry_operation}><PropertyName>{self.geometry_property_name}</PropertyName>{geom_gml}</{geometry_operation}></Filter>"
return spatial_filter
@ -163,7 +164,28 @@ class ParcelWFSFetcher(AbstractWFSFetcher):
if rerun_on_exception:
# Wait a second before another try
sleep(1)
self.get_features(
return self.get_features(
typenames,
spatial_operator,
filter_srid,
start_index,
rerun_on_exception=False
)
else:
e.msg += content
raise e
try:
fetched_features = content.get(
"features",
{},
)
except AttributeError as e:
# Might occur if returned content is no json but something different (maybe an error message)
if rerun_on_exception:
# Wait a second before another try
sleep(1)
return self.get_features(
typenames,
spatial_operator,
filter_srid,
@ -173,10 +195,6 @@ class ParcelWFSFetcher(AbstractWFSFetcher):
else:
e.msg += content
raise e
fetched_features = content.get(
"features",
{},
)
found_features += fetched_features
@ -187,4 +205,7 @@ class ParcelWFSFetcher(AbstractWFSFetcher):
# If a 'full' response returned, there might be more to fetch. Increase the start_index!
start_index += self.count
if not found_features:
print(f"No features found. Fetched content: {content}\nUsed POST body: {post_body}")
return found_features