将(多)多边形的质心保存为模型中的点几何
Saving centroid of a (multi)polygon as point geometry in a model
我有两个 table,一个是多边形几何图形,另一个是点几何图形列。我希望将所选多边形的质心保存为另一个 table.
的点几何
class matview_all_administrative_units(models.Model):
lau_id = models.IntegerField(primary_key=True)
ortsgemeinde = models.CharField(max_length=150)
verwaltungsgemeinde = models.CharField(max_length=150)
landkreis_bezirk = models.CharField(max_length=150)
bundesland_kanton = models.CharField(max_length=150)
staat = models.CharField(max_length=150)
geom = models.MultiPolygonField(srid=4326)
class Meta:
managed = False
db_table = 'administrative_hierarchy_full_geom'
class site(models.Model):
sid = models.AutoField(primary_key=True)
site_name = models.CharField(max_length=250)
site_notes = models.CharField(max_length=2500, blank=True, null=True)
municipality = models.ForeignKey('local_administrative_unit', on_delete=models.PROTECT)
geom = models.PointField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.sid, self.site_name, self.municipality)
要添加新站点,现有行政单位必须与其相关联,并且其多边形的中心应用作站点的 location/geometry。现在我做了这个:
class NewSiteView(CreateView):
model = models.site
form_class = forms.NewSiteForm
template_name = 'datamanager/newsite.html'
success_url = '/sites/'
调用此表单:
from django.forms import ModelForm, HiddenInput
from django.contrib.gis.db.models.functions import Centroid
from . import models
class NewSiteForm(ModelForm):
class Meta:
model = models.site
fields = ['site_name', 'site_notes', 'municipality','geom']
widgets = {
'geom': HiddenInput(),
}
def clean(self):
super().clean()
self.cleaned_data['geom'] = Centroid(models.matview_all_administrative_units.objects.values('geom').filter(lau_id=self.cleaned_data['municipality'].lau_id))
但是,这会导致此错误:
所以我基本上不是在计算一个点,而是在计算一个 'centroid object' - 到目前为止一切顺利,django 文档告诉我们这一点。现在我被困在试图从这个质心东西中得到一些东西,我可以把它推到那个几何列中。据我所知,我获取了正确的数据并将其处理到正确的函数(否则我认为错误应该出现在这段代码的前面?),但结果对于将其插入到几何列中没有用。那么我如何理解我的观点呢? (笑)
终于找到解决办法了。将此添加到 CreateView:
def form_valid(self, form):
pol = models.local_administrative_unit.objects.values('geom').filter(lau_id=form.cleaned_data['municipality'].lau_id)[0]['geom']
cent_point = pol.centroid
form.instance.geom = cent_point.wkt
form.save()
return super().form_valid(form)
我从多边形中获取几何图形,计算它的质心并将它的几何图形作为 well-known 文本插入到表单中,然后保存表单。
我有两个 table,一个是多边形几何图形,另一个是点几何图形列。我希望将所选多边形的质心保存为另一个 table.
的点几何class matview_all_administrative_units(models.Model):
lau_id = models.IntegerField(primary_key=True)
ortsgemeinde = models.CharField(max_length=150)
verwaltungsgemeinde = models.CharField(max_length=150)
landkreis_bezirk = models.CharField(max_length=150)
bundesland_kanton = models.CharField(max_length=150)
staat = models.CharField(max_length=150)
geom = models.MultiPolygonField(srid=4326)
class Meta:
managed = False
db_table = 'administrative_hierarchy_full_geom'
class site(models.Model):
sid = models.AutoField(primary_key=True)
site_name = models.CharField(max_length=250)
site_notes = models.CharField(max_length=2500, blank=True, null=True)
municipality = models.ForeignKey('local_administrative_unit', on_delete=models.PROTECT)
geom = models.PointField(srid=4326)
def __str__(self):
return '{}, {} ({})'.format(self.sid, self.site_name, self.municipality)
要添加新站点,现有行政单位必须与其相关联,并且其多边形的中心应用作站点的 location/geometry。现在我做了这个:
class NewSiteView(CreateView):
model = models.site
form_class = forms.NewSiteForm
template_name = 'datamanager/newsite.html'
success_url = '/sites/'
调用此表单:
from django.forms import ModelForm, HiddenInput
from django.contrib.gis.db.models.functions import Centroid
from . import models
class NewSiteForm(ModelForm):
class Meta:
model = models.site
fields = ['site_name', 'site_notes', 'municipality','geom']
widgets = {
'geom': HiddenInput(),
}
def clean(self):
super().clean()
self.cleaned_data['geom'] = Centroid(models.matview_all_administrative_units.objects.values('geom').filter(lau_id=self.cleaned_data['municipality'].lau_id))
但是,这会导致此错误:
所以我基本上不是在计算一个点,而是在计算一个 'centroid object' - 到目前为止一切顺利,django 文档告诉我们这一点。现在我被困在试图从这个质心东西中得到一些东西,我可以把它推到那个几何列中。据我所知,我获取了正确的数据并将其处理到正确的函数(否则我认为错误应该出现在这段代码的前面?),但结果对于将其插入到几何列中没有用。那么我如何理解我的观点呢? (笑)
终于找到解决办法了。将此添加到 CreateView:
def form_valid(self, form):
pol = models.local_administrative_unit.objects.values('geom').filter(lau_id=form.cleaned_data['municipality'].lau_id)[0]['geom']
cent_point = pol.centroid
form.instance.geom = cent_point.wkt
form.save()
return super().form_valid(form)
我从多边形中获取几何图形,计算它的质心并将它的几何图形作为 well-known 文本插入到表单中,然后保存表单。