Exemplo n.º 1
0
    def aggregate_periods(self, periods):
        """Returns list of ndarrays averaged to a given number of periods.

        Arguments:
        periods -- desired number of periods as int
        """
        try:
            fieldname = self.raster_field.name
        except TypeError:
            raise exceptions.FieldDoesNotExist('Raster field not found')
        arrays = self.arrays(fieldname)
        arr = arrays[0]
        fill = getattr(arr, 'fill_value', None)
        if getattr(arr, 'ndim', 0) > 2:
            arrays = np.vstack(arrays)
        marr = np.ma.array(arrays, fill_value=fill, copy=False)
        # Try to reshape using equal sizes first and fall back to unequal
        # splits.
        try:
            means = marr.reshape((periods, -1)).mean(axis=1)
        except ValueError:
            means = [a.mean() for a in np.array_split(marr, periods)]
        obj = self[0]
        setattr(obj, fieldname, means)
        return [obj]
Exemplo n.º 2
0
 def get_related_field(self):
     """
     Return the Field in the 'to' object to which this relationship is tied.
     """
     field = self.model._meta.get_field(self.field_name)
     if not field.concrete:
         raise exceptions.FieldDoesNotExist("No related field named '%s'" % self.field_name)
     return field
Exemplo n.º 3
0
 def to_internal_value(self, data):
     if sc.has_features(data):
         for feat in data['features']:
             return self.to_internal_value(feat)
     try:
         sref = SpatialReference(data['crs']['properties']['name'])
     except KeyError:
         sref = None
     # Force evaluation of fields property.
     if not self.fields and self.Meta.geom_field is None:
         raise exceptions.FieldDoesNotExist('Geometry field not found')
     record = {self.Meta.geom_field: data.get('geometry')}
     record.update(data.get('properties', {}))
     feature = super(FeatureSerializer, self).to_internal_value(record)
     if feature and sref:
         geom = feature[self.Meta.geom_field]
         geom.srid = sref.srid
     return feature
Exemplo n.º 4
0
def geo_field(queryset):
    """Returns the GeometryField for a django or spillway GeoQuerySet."""
    for field in queryset.model._meta.fields:
        if isinstance(field, models.GeometryField):
            return field
    raise exceptions.FieldDoesNotExist('No GeometryField found')