-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GeometrySizeValidator to limit area
- Loading branch information
Showing
2 changed files
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django import forms | ||
from django.utils.deconstruct import deconstructible | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
@deconstructible | ||
class GeometrySizeValidator(object): | ||
"""Field validator to limit geometries to a given area.""" | ||
message = _('Max area exceeded.') | ||
code = 'invalid' | ||
|
||
def __init__(self, max_area, srid=None): | ||
self.max_area = max_area | ||
self.srid = srid | ||
|
||
def __call__(self, geom): | ||
if self.srid and geom.srid != self.srid: | ||
geom.transform(self.srid) | ||
if (getattr(geom, 'ogr', geom).dimension > 1 and | ||
geom.area > self.max_area): | ||
raise forms.ValidationError(self.message, code=self.code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters