Dynamic Map Web Forms
How to use Python, GeoDjango, and OpenLayers to Create a Form that you can use to modify Geographic Features
Recently a draw polygon feature was added to COOPGIS. To do so elegantly required using a form that a user could click on to create the points of the polygon. The below are some snippets that should help explain how to do so.
Simplified Django Model, with GeoModelAdmin.
@class StakerQuickGeneratePolygon(models.Model):
Polygon=models.PolygonField(db_column='the_geom', verbose_name=('polygon'), srid=djangoproject.settings.COOPGISSRID, null=True, blank=True)
objects=models.GeoManager()
class Meta:
app_label='djangoproject'
class StakerQuickGeneratePolygonAdmin(admin.GeoModelAdmin):
list_filter=('polygon',)
list_display=('object', 'polygon')@
Build a dynamic form using the Django dynamic form snippet. Code to setup the widget and build the form included:
geoAdmin=StakerQuickGeneratePolygonAdmin(StakerQuickGeneratePolygon, admin.site)
PolygonFormField=StakerQuickGeneratePolygon._meta.get_field('Polygon')
PolygonWidget=geoAdmin.get_map_widget(PolygonFormField)
Dict['Polygon']=forms.CharField(widget=PolygonWidget())
To process the form I used the line:
polygon=fromstr(form.data['Polygon'])
From there, it was just using Django Python code to update the polygon, get a list of features within it, and otherwise return information to the user.
This polygon feature is used to more quickly make additions to a Staker Work Order in a defined area and should be available in the version of COOPGIS planned to be released around the end of the year.
Comments