序列化geodjano对象时更改coodinates精度

时间:2019-07-04 08:47:53

标签: geojson geodjango django-serializer

我有如下的GeoDjango多边形模型

class PolygonFeature(models.Model):
    shapefile = models.ForeignKey(Shapefile,
                             on_delete=models.CASCADE, related_name='polygon_shp')
    geom = models.PolygonField(srid=4326, blank=True, null=True)

    def __str__(self):
        return str(self.shapefile.filename) + "-" + str(self.pk)

我正在通过LayerMapping向模型添加多边形,其中的shapefile是

mapping = {
'shapefile':{'id':'survey_pk'},
'geom': geom,
}
lm = LayerMapping(model, out, mapping, transform=transform, encoding='iso-8859-1')
lm.save(verbose=False)

在将多边形对象几何序列化为GeoJSOn时,我需要更改坐标精度

feat = PolygonFeature.objects.filter(shapefile=obj)
json_obj = serialize('geojson', feat)

当前

 {
"type": "FeatureCollection",
"crs": {
  "type": "name",
  "name": "EPSG:4326"
},
"features": [
  {
    "type": "Feature",
    "properties": {
      "shapefile": 53,
      "pk": "45269"
    },
    "geometry": {
      "type": "Polygon",
      "coordinates": [
        [
          [
            75.6083423241,
            31.315054275
          ],
          [
            75.5543398801,
            31.30543373742
          ],
          [
            75.5970812345,
            31.2980635331
          ],
          [
            75.6543504465,
            31.30345354185
          ],
          [
            75.6035348241,
            31.315035375
          ]
        ]
      ]
    }
  }
]

}

1 个答案:

答案 0 :(得分:0)

在序列化类中,我使用precision参数使其起作用:

class FeatBaseSerializer(GeoFeatureModelSerializer):
    editor = PkListField(read_only=True)
    geom = GeometryField(precision=8)