示例#1
0
class UsStates(Model):
    state = CharField(max_length=2)
    name = CharField(max_length=24)
    fips = CharField(max_length=2)
    lon = FloatField()
    lat = FloatField()
    geom = MultiPolygonField(srid=4326)

    # Returns the string representation of the model.
    def __str__(self):  # __unicode__ on Python 2
        return self.name
示例#2
0
class Flat(Estate):
    living_area = FloatField(null=True)
    kitchen_area = FloatField(null=True)
    rooms = SmallIntegerField()
    floor = SmallIntegerField()
    total_floor = SmallIntegerField()
    ceiling_height = FloatField(null=True)
    details = ManyToManyField(Detail, db_table='flats_details')
    saved_field = 'saved_flats'
    lookups = {
        'state': 'geolocation__state',
        'locality': 'geolocation__locality',
        'county': 'geolocation__county',
        'neighbourhood': 'geolocation__neighbourhood',
        'road': 'geolocation__road',
        'house_number': 'geolocation__house_number',
        'area_from': 'area__gte',
        'area_to': 'area__lte',
        'living_area_from': 'living_area__gte',
        'living_area_to': 'living_area__lte',
        'kitchen_area_from': 'kitchen_area__gte',
        'kitchen_area_to': 'kitchen_area__lte',
        'rooms_from': 'rooms__gte',
        'rooms_to': 'rooms__lte',
        'floor_from': 'floor__gte',
        'floor_to': 'floor__lte',
        'total_floor_from': 'total_floor__gte',
        'total_floor_to': 'total_floor__lte',
        'ceiling_height_from': 'ceiling_height__gte',
        'ceiling_height_to': 'ceiling_height__lte'
    }
    order_by = {'area', '-area', 'rooms', '-rooms', 'price', '-price'}

    class Meta:
        db_table = 'flats'
        constraints = [
            UniqueConstraint(
                fields=['geolocation_id', 'rooms', 'floor', 'total_floor'],
                name='flat_geolocation_id_rooms_floor_total_floor_key'
            )
        ]
示例#3
0
class WorldBorder(Model):
    # Regular Django fields corresponding to the attributes in the
    # world borders shapefile.
    name = CharField(max_length=50)
    area = IntegerField()
    pop2005 = IntegerField('Population 2005')
    fips = CharField('FIPS Code', max_length=2)
    iso2 = CharField('2 Digit ISO', max_length=2)
    iso3 = CharField('3 Digit ISO', max_length=3)
    un = IntegerField('United Nations Code')
    region = IntegerField('Region Code')
    subregion = IntegerField('Sub-Region Code')
    lon = FloatField()
    lat = FloatField()

    # GeoDjango-specific: a geometry field (MultiPolygonField), and
    # overriding the default manager with a GeoManager instance.
    mpoly = MultiPolygonField()

    # Returns the string representation of the model.
    def __str__(self):  # __unicode__ on Python 2
        return self.name
示例#4
0
class Estate(Model):
    url = URLField(max_length=400, unique=True)
    avatar = URLField(max_length=400, null=True)
    published = DateField()
    geolocation = ForeignKey(Geolocation, on_delete=CASCADE)
    price = DecimalField(max_digits=10, decimal_places=2)
    rate = DecimalField(max_digits=10, decimal_places=2)
    area = FloatField()
    is_visible = BooleanField(default=True)
    lookups = {}
    order_by = set()

    class Meta:
        abstract = True

    def __str__(self) -> str:
        return f'[{self.url}, {self.area}, {self.rate}]'
示例#5
0
class HausdorffDistance(GeoFunc):
    geom_param_pos = (0, 1)
    output_field = FloatField()
示例#6
0
class ST_Area(Func):
    function = 'ST_Area'
    output_field = FloatField()
示例#7
0
class ST_Length(Func):
    function = 'ST_Length'
    output_field = FloatField()
示例#8
0
class ST_HausdorffDistance(Func):
    function = 'ST_HausdorffDistance'
    output_field = FloatField()
示例#9
0
class ST_LineLocatePoint(Func):
    function = 'ST_LineLocatePoint'
    output_field = FloatField()
示例#10
0
class Place(Base):

    # attribution
    attribution = TextField(null=True, blank=True, db_index=DB_INDEX)

    # concordances
    enwiki_title = TextField(null=True, blank=True, db_index=DB_INDEX)
    geonames_id = IntegerField(null=True, blank=True, db_index=DB_INDEX)
    osm_id = TextField(null=True, blank=True, db_index=DB_INDEX)
    pcode = TextField(null=True, blank=True, db_index=DB_INDEX)
    fips = IntegerField(null=True, blank=True, db_index=DB_INDEX)

    # admin stuff
    admin1_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    admin2_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    admin3_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    admin4_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    admin_level = IntegerField(null=True, blank=True, db_index=DB_INDEX)

    # bounding box stuff
    east = FloatField(null=True, blank=True)
    north = FloatField(null=True, blank=True)
    south = FloatField(null=True, blank=True)
    west = FloatField(null=True, blank=True)

    # name stuff
    name = TextField(null=True, blank=True, db_index=DB_INDEX)
    name_ascii = TextField(null=True, blank=True, db_index=DB_INDEX)
    name_display = TextField(null=True, blank=True, db_index=DB_INDEX)
    name_en = TextField(null=True, blank=True, db_index=DB_INDEX)
    name_normalized = TextField(null=True, blank=True, db_index=DB_INDEX)
    other_names = TextField(null=True, blank=True, db_index=False)

    # place types
    geonames_feature_class = TextField(null=True, blank=True, db_index=DB_INDEX)
    geonames_feature_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    place_type = TextField(null=True, blank=True, db_index=DB_INDEX)

    # geometries
    objects = GeoManager()
    latitude = FloatField(null=True, blank=True)
    longitude = FloatField(null=True, blank=True)
    mls = MultiLineStringField(null=True, blank=True)
    mpoly = MultiPolygonField(null=True, blank=True)
    point = PointField(null=True, blank=True)
    area_sqkm = IntegerField(null=True, blank=True)

    # osm stuff
    importance = FloatField(null=True, blank=True)
    osmname_class = TextField(null=True, blank=True, db_index=DB_INDEX)
    osmname_type = TextField(null=True, blank=True, db_index=DB_INDEX)
    osm_type = TextField(null=True, blank=True, db_index=DB_INDEX)
    place_rank = IntegerField(null=True, blank=True, db_index=DB_INDEX)

    # dem and elevation stuff
    dem = FloatField(null=True, blank=True)
    elevation = FloatField(null=True, blank=True)

    # geocoder stuff
    city = TextField(null=True, blank=True, db_index=DB_INDEX)
    county = TextField(null=True, blank=True, db_index=DB_INDEX) # should be 100
    country = TextField(null=True, blank=True, db_index=DB_INDEX)
    country_code = TextField(null=True, blank=True, db_index=DB_INDEX)
    state = TextField(null=True, blank=True, db_index=DB_INDEX)
    street = TextField(null=True, blank=True, db_index=DB_INDEX)

    #misc
    note = TextField(null=True, blank=True)
    population = BigIntegerField(null=True, blank=True, db_index=DB_INDEX)
    # number of times name appeared and meant this place minus number of times didn't mean this place
    popularity = BigIntegerField(null=True, blank=True, db_index=DB_INDEX)
    timezone = TextField(null=True, blank=True, db_index=DB_INDEX)
    topic = ForeignKey("Topic", null=True, on_delete=SET_NULL, db_index=DB_INDEX) # represents the most common topic associated with this place
    wikidata_id = TextField(null=True, blank=True, db_index=DB_INDEX)

    def get_all_names(self):
        if not hasattr(self, "all_names"):
            names = set()
            names.add(self.name)
            names.add(self.name_ascii)
            names.add(self.name_display)
            names.add(self.name_en)
            names.add(self.name_normalized)
            if self.other_names:
                for other_name in self.other_names.split(","):
                    names.add(other_name)
            names.discard(None)
            self.all_names = names
        return self.all_names

    class Meta:
        ordering = ['name']
示例#11
0
class LayerImage(Model):
    """
    Geospatial image uploaded by the user.

    `meta_json` may be populated during geoprocessing and should contain
    a serialized JSON blob of image metadata.
    This blob should be in the form of key value pairs (object literal)
    and will be displayed as-is.
    """
    layer = ForeignKey(Layer, related_name='layer_images')
    priority = IntegerField(
        default=0,
        help_text='The order which images are layered (starting from 0)')
    thumb_small_key = CharField(max_length=255,
                                blank=True,
                                default='',
                                help_text='S3 key for small thumbnail')
    thumb_large_key = CharField(max_length=255,
                                blank=True,
                                default='',
                                help_text='S3 key for large thumbnail')
    meta_json = TextField(
        null=True,
        blank=True,
        help_text='Serialized JSON of image metadata',
    )

    min_x = FloatField(default=None, blank=True, null=True)
    max_x = FloatField(default=None, blank=True, null=True)
    min_y = FloatField(default=None, blank=True, null=True)
    max_y = FloatField(default=None, blank=True, null=True)

    file_name = CharField(
        blank=False,
        default='',
        max_length=255,
        help_text='Filename of original file',
    )
    s3_uuid = UUIDField(default=uuid.uuid4, editable=False)
    file_extension = CharField(blank=False,
                               default='',
                               max_length=10,
                               help_text='Extension of file')
    bucket_name = CharField(blank=False,
                            default='',
                            max_length=255,
                            help_text='Name of S3 bucket')
    source_s3_bucket_key = CharField(
        blank=True,
        null=True,
        max_length=255,
        help_text='S3 <bucket>/<key> for source image (optional)')

    status_created = DateTimeField(auto_now_add=True)
    status_transfer_start = DateTimeField(null=True, blank=True)
    status_transfer_end = DateTimeField(null=True, blank=True)
    status_validate_start = DateTimeField(null=True, blank=True)
    status_validate_end = DateTimeField(null=True, blank=True)
    status_thumbnail_start = DateTimeField(null=True, blank=True)
    status_thumbnail_end = DateTimeField(null=True, blank=True)

    status_upload_error = CharField(max_length=255, blank=True, null=True)
    status_transfer_error = CharField(max_length=255, blank=True, null=True)
    status_validate_error = CharField(max_length=255, blank=True, null=True)
    status_thumbnail_error = CharField(max_length=255, blank=True, null=True)

    def reset(self):
        """
        Resets fields to prepare for a retry.
        """
        self.status_transfer_start = None
        self.status_transfer_end = None
        self.status_validate_start = None
        self.status_validate_end = None
        self.status_thumbnail_start = None
        self.status_thumbnail_end = None

        self.status_upload_error = None
        self.status_transfer_error = None
        self.status_validate_error = None
        self.status_thumbnail_error = None

        self.save()

    def has_been_validated(self):
        return self.status_validate_end is not None

    def is_copy_image(self):
        return self.source_s3_bucket_key is not None

    def get_s3_key(self):
        return '%d-%s.%s' % (self.layer.user.id, self.s3_uuid,
                             self.file_extension)

    def get_s3_uri(self):
        return 's3://{}/{}-{}.{}'.format(self.bucket_name, self.layer.user_id,
                                         self.s3_uuid, self.file_extension)

    def to_json(self):
        return {
            'id': self.id,
            'thumb_small': generate_thumb_url(self.thumb_small_key),
            'thumb_large': generate_thumb_url(self.thumb_large_key),
            'meta_json': self.meta_json,
            'min_x': self.min_x,
            'max_x': self.max_x,
            'min_y': self.min_y,
            'max_y': self.max_y,
            'file_name': self.file_name,
            's3_uuid': str(self.s3_uuid),
            'file_extension': self.file_extension,
            'bucket_name': self.bucket_name,
            'source_s3_bucket_key': self.source_s3_bucket_key,
            'status_created': self.status_created is not None,
            'status_transfer_start': self.status_transfer_start is not None,
            'status_transfer_end': self.status_transfer_end is not None,
            'status_validate_start': self.status_validate_start is not None,
            'status_validate_end': self.status_validate_end is not None,
            'status_thumbnail_start': self.status_thumbnail_start is not None,
            'status_thumbnail_end': self.status_thumbnail_end is not None,
            'status_upload_error': self.status_upload_error,
            'status_transfer_error': self.status_transfer_error,
            'status_validate_error': self.status_validate_error,
            'status_thumbnail_error': self.status_thumbnail_error,
            'source_s3_bucket_key': self.source_s3_bucket_key
        }

    def update_status_start(self, status):
        value = datetime.now()
        if status == enums.STATUS_TRANSFER:
            self.status_transfer_start = (value
                                          if self.status_transfer_start is None
                                          else self.status_transfer_start)
        elif status == enums.STATUS_VALIDATE:
            self.status_validate_start = (value
                                          if self.status_validate_start is None
                                          else self.status_validate_start)
        elif status == enums.STATUS_THUMBNAIL:
            self.status_thumbnail_start = (value if
                                           self.status_thumbnail_start is None
                                           else self.status_thumbnail_start)

    def update_status_end(self, status, error_message=None):
        value = datetime.now()
        if status == enums.STATUS_TRANSFER:
            self.status_transfer_end = value
            self.status_transfer_error = error_message
        elif status == enums.STATUS_VALIDATE:
            self.status_validate_end = value
            self.status_validate_error = error_message
        elif status == enums.STATUS_THUMBNAIL:
            self.status_thumbnail_end = value
            self.status_thumbnail_error = error_message

    def set_bounds(self, bounds):
        self.min_x = bounds[0]
        self.max_x = bounds[1]
        self.min_y = bounds[2]
        self.max_y = bounds[3]
        self.save(update_fields=['min_x', 'max_x', 'min_y', 'max_y'])
示例#12
0
class Media(Model):
    """
    Fields:

    original: Originally uploaded file. Users cannot interact with it except
              by downloading it.

              .. deprecated :: Use media_files object

    segment_info: File for segment files to support MSE playback.

                  .. deprecated :: Use meda_files instead

    media_files: Dictionary to contain a map of all files for this media.
                 The schema looks like this:

                 .. code-block ::

                     map = {"archival": [ VIDEO_DEF, VIDEO_DEF,... ],
                            "streaming": [ VIDEO_DEF, VIDEO_DEF, ... ],
                            <"audio": [AUDIO_DEF]>}
                     video_def = {"path": <path_to_disk>,
                                  "codec": <human readable codec>,
                                  "resolution": [<vertical pixel count, e.g. 720>, width]
                     audio_def = {"path": <path_to_disk>,
                                  "codec": <human readable codec>}


                                  ###################
                                  # Optional Fields #
                                  ###################

                                  # Path to the segments.json file for streaming files.
                                  # not expected/required for archival. Required for
                                  # MSE playback with seek support for streaming files.
                                  segment_info = <path_to_json>

                                  # If supplied will use this instead of currently
                                  # connected host. e.g. https://example.com
                                  "host": <host url>
                                  # If specified will be used for HTTP authorization
                                  # in the request for media. I.e. "bearer <token>"
                                  "http_auth": <http auth header>

                                  # Example mime: 'video/mp4; codecs="avc1.64001e"'
                                  # Only relevant for straming files, will assume
                                  # example above if not present.
                                  "codec_mime": <mime for MSE decode>

                                  "codec_description": <description other than codec>}


    """
    project = ForeignKey(Project,
                         on_delete=SET_NULL,
                         null=True,
                         blank=True,
                         db_column='project')
    meta = ForeignKey(MediaType,
                      on_delete=SET_NULL,
                      null=True,
                      blank=True,
                      db_column='meta')
    """ Meta points to the defintion of the attribute field. That is
        a handful of AttributeTypes are associated to a given MediaType
        that is pointed to by this value. That set describes the `attribute`
        field of this structure. """
    attributes = JSONField(null=True, blank=True)
    """ Values of user defined attributes. """
    gid = CharField(max_length=36, null=True, blank=True)
    """ Group ID for the upload that created this media. Note we intentionally do
        not use UUIDField because this field is provided by the uploader and not
        guaranteed to be an actual UUID. """
    uid = CharField(max_length=36, null=True, blank=True)
    """ Unique ID for the upload that created this media. Note we intentionally do
        not use UUIDField because this field is provided by the uploader and not
        guaranteed to be an actual UUID. """
    created_datetime = DateTimeField(auto_now_add=True, null=True, blank=True)
    created_by = ForeignKey(User,
                            on_delete=SET_NULL,
                            null=True,
                            blank=True,
                            related_name='media_created_by',
                            db_column='created_by')
    modified_datetime = DateTimeField(auto_now=True, null=True, blank=True)
    modified_by = ForeignKey(User,
                             on_delete=SET_NULL,
                             null=True,
                             blank=True,
                             related_name='media_modified_by',
                             db_column='modified_by')
    name = CharField(max_length=256)
    md5 = SlugField(max_length=32)
    """ md5 hash of the originally uploaded file. """
    file = FileField(null=True, blank=True)
    last_edit_start = DateTimeField(null=True, blank=True)
    """ Start datetime of a session in which the media's annotations were edited.
    """
    last_edit_end = DateTimeField(null=True, blank=True)
    """ End datetime of a session in which the media's annotations were edited.
    """
    original = FilePathField(path=settings.RAW_ROOT, null=True, blank=True)
    thumbnail = ImageField(null=True, blank=True)
    thumbnail_gif = ImageField(null=True, blank=True)
    num_frames = IntegerField(null=True, blank=True)
    fps = FloatField(null=True, blank=True)
    codec = CharField(null=True, blank=True, max_length=256)
    width = IntegerField(null=True)
    height = IntegerField(null=True)
    segment_info = FilePathField(path=settings.MEDIA_ROOT,
                                 null=True,
                                 blank=True)
    media_files = JSONField(null=True, blank=True)

    def update_media_files(self, media_files):
        """ Updates media files by merging a new key into existing JSON object.
        """
        # Handle null existing value.
        if self.media_files is None:
            self.media_files = {}

        # Append to existing definitions.
        new_streaming = media_files.get('streaming', [])
        old_streaming = self.media_files.get('streaming', [])
        streaming = new_streaming + old_streaming
        new_archival = media_files.get('archival', [])
        old_archival = self.media_files.get('archival', [])
        archival = new_archival + old_archival
        new_audio = media_files.get('audio', [])
        old_audio = self.media_files.get('audio', [])
        audio = new_audio + old_audio

        for fp in new_streaming:
            path = fp['path']
            seg_path = fp['segment_info']
            Resource.add_resource(path)
            Resource.add_resource(seg_path)

        for fp in new_archival:
            Resource.add_resource(fp['path'])

        for fp in new_audio:
            Resource.add_resource(fp['path'])

        # Only fill in a key if it has at least one definition.
        self.media_files = {}
        if streaming:
            streaming.sort(key=lambda x: x['resolution'][0], reverse=True)
            self.media_files['streaming'] = streaming
        if archival:
            self.media_files['archival'] = archival
        if audio:
            self.media_files['audio'] = audio

        # Handle roi, layout, and quality
        for x in ['layout', 'ids', 'quality']:
            if x in media_files:
                self.media_files[x] = media_files[x]
示例#13
0
class Layer(Model):
    """
    Represents a single Image Layer which may contain one or more
    geospatial images.
    """
    class Meta:
        unique_together = ('user', 'name')

    user = ForeignKey(User)
    name = CharField(max_length=255)
    slug = SlugField(max_length=255, blank=True)

    status_created = DateTimeField(auto_now_add=True)
    status_validate_start = DateTimeField(null=True, blank=True)
    status_validate_end = DateTimeField(null=True, blank=True)
    status_thumbnail_start = DateTimeField(null=True, blank=True)
    status_thumbnail_end = DateTimeField(null=True, blank=True)
    status_create_cluster_start = DateTimeField(null=True, blank=True)
    status_create_cluster_end = DateTimeField(null=True, blank=True)
    status_chunk_start = DateTimeField(null=True, blank=True)
    status_chunk_end = DateTimeField(null=True, blank=True)
    status_mosaic_start = DateTimeField(null=True, blank=True)
    status_mosaic_end = DateTimeField(null=True, blank=True)
    status_failed = DateTimeField(null=True, blank=True)
    status_completed = DateTimeField(null=True, blank=True)
    status_heartbeat = DateTimeField(null=True, blank=True)

    status_validate_error = CharField(max_length=255, blank=True, null=True)
    status_thumbnail_error = CharField(max_length=255, blank=True, null=True)
    status_create_cluster_error = CharField(max_length=255,
                                            blank=True,
                                            null=True)
    status_chunk_error = CharField(max_length=255, blank=True, null=True)
    status_mosaic_error = CharField(max_length=255, blank=True, null=True)
    status_failed_error = CharField(max_length=255, blank=True, null=True)

    description = TextField(blank=True)
    organization = CharField(max_length=255, blank=True, null=True)

    is_public = BooleanField(default=False)

    capture_start = DateField(blank=True, null=True)
    capture_end = DateField(blank=True, null=True)

    min_x = FloatField(default=None, blank=True, null=True)
    max_x = FloatField(default=None, blank=True, null=True)
    min_y = FloatField(default=None, blank=True, null=True)
    max_y = FloatField(default=None, blank=True, null=True)

    area = FloatField(default=0, blank=True, null=True)
    area_unit = CharField(
        blank=True,
        max_length=18,
        choices=enums.AREA_UNIT_CHOICES,
        default=enums.SQ_KM,
    )
    projection = CharField(
        blank=True,
        max_length=18,
        choices=enums.PROJECTION_CHOICES,
        default=enums.WGS84,
        help_text='Source Projection',
    )
    srid = CharField(
        blank=True,
        max_length=18,
        choices=enums.SRID_CHOICES,
        default=enums.WGS84,
        help_text='Source SRS',
    )

    tile_srid = CharField(blank=True,
                          max_length=18,
                          choices=enums.SRID_CHOICES,
                          default=enums.WGS84,
                          help_text='Tile SRS')
    tile_format = CharField(
        blank=True,
        max_length=18,
        choices=enums.TILE_FORMAT_CHOICES,
        default=enums.OVER_PNG32,
    )
    tile_origin = CharField(
        blank=True,
        max_length=18,
        choices=enums.TILE_ORIGIN_CHOICES,
        default=enums.TOPLEFT,
        help_text='Tiling Scheme',
    )
    resampling = CharField(
        blank=True,
        max_length=18,
        choices=enums.TILE_RESAMPLING_CHOICES,
        default=enums.BILINEAR,
    )
    transparency = CharField(
        blank=True,
        max_length=18,
        help_text='Hexadecimal (Ex. #00FF00)',
    )

    dismissed = BooleanField(blank=True, default=False)

    created_at = DateTimeField(auto_now_add=True)
    updated_at = DateTimeField(auto_now=True)
    deleted_at = DateTimeField(null=True, blank=True)
    status_updated_at = DateTimeField(default=datetime.now)

    thumb_small_key = CharField(max_length=255,
                                blank=True,
                                default='',
                                help_text='S3 key for small thumbnail')
    thumb_large_key = CharField(max_length=255,
                                blank=True,
                                default='',
                                help_text='S3 key for large thumbnail')

    def has_copy_images(self):
        for image in self.layer_images.all():
            if image.is_copy_image():
                return True
        return False

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Layer, self).save(*args, **kwargs)

    def to_json(self):
        """
        Return JSON serializable model data.

        Note: Prefetch all related foreign key relationships before
        calling this method for optimal performance.
        """
        tags = [m.to_json() for m in self.layer_tags.all()]
        images = [m.to_json() for m in self.layer_images.all()]

        capture_start = self.capture_start.isoformat() \
            if self.capture_start else None
        capture_end = self.capture_end.isoformat() \
            if self.capture_end else None

        return {
            'id':
            self.id,
            'name':
            self.name,
            'slug':
            self.slug,
            'description':
            self.description,
            'organization':
            self.organization,
            'is_public':
            self.is_public,
            'capture_start':
            capture_start,
            'capture_end':
            capture_end,
            'area':
            self.area,
            'area_unit':
            self.area_unit,
            'bounds':
            self.get_bounds(),
            'projection':
            self.projection,
            'srid':
            self.srid,
            'tile_srid':
            self.tile_srid,
            'tile_format':
            self.tile_format,
            'tile_origin':
            self.tile_origin,
            'resampling':
            self.resampling,
            'transparency':
            self.transparency,
            'status_created':
            self.status_created is not None,
            'status_validate_start':
            self.status_validate_start is not None,
            'status_validate_end':
            self.status_validate_end is not None,
            'status_thumbnail_start':
            self.status_thumbnail_start is not None,
            'status_thumbnail_end':
            self.status_thumbnail_end is not None,
            'status_create_cluster_start': (self.status_create_cluster_start
                                            is not None),
            'status_create_cluster_end': (self.status_create_cluster_end
                                          is not None),
            'status_chunk_start':
            self.status_chunk_start is not None,
            'status_chunk_end':
            self.status_chunk_end is not None,
            'status_mosaic_start':
            self.status_mosaic_start is not None,
            'status_mosaic_end':
            self.status_mosaic_end is not None,
            'status_failed':
            self.status_failed is not None,
            'status_completed':
            self.status_completed is not None,
            'status_heartbeat':
            self.status_completed is not None,
            'status_validate_error':
            self.status_validate_error,
            'status_thumbnail_error':
            self.status_thumbnail_error,
            'status_create_cluster_error':
            self.status_create_cluster_error,
            'status_chunk_error':
            self.status_chunk_error,
            'status_mosaic_error':
            self.status_mosaic_error,
            'status_failed_error':
            self.status_failed_error,
            'created_at':
            self.created_at.isoformat(),
            'updated_at':
            self.updated_at.isoformat(),
            'status_updated_at':
            self.created_at.isoformat(),
            'thumb_small':
            generate_thumb_url(self.thumb_small_key),
            'thumb_large':
            generate_thumb_url(self.thumb_large_key),

            # Foreign key fields
            'tags':
            tags,
            'images':
            images,
            'username':
            self.user.username,

            # Generated fields
            'url':
            self.get_absolute_url(),
            'meta_url':
            self.get_meta_url(),
            'favorite_url':
            self.get_favorite_url(),
            'dismiss_url':
            self.get_dismiss_url(),
            'retry_url':
            self.get_retry_url(),
            'tile_url':
            self.get_tile_url(),
        }

    def get_bounds(self):
        if all([self.min_x, self.min_y, self.max_x, self.max_y]):
            return [
                [self.min_y, self.min_x],
                [self.max_y, self.max_x],
            ]
        return None

    def retry_possible(self):
        """
        Returns true if it is possible to retry processing a layer.
        """
        return self.status_failed is not None

    def reset(self):
        """
        Resets fields to prepare for a retry.
        """
        self.status_validate_start = None
        self.status_validate_end = None
        self.status_thumbnail_start = None
        self.status_thumbnail_end = None
        self.status_create_cluster_start = None
        self.status_create_cluster_end = None
        self.status_chunk_start = None
        self.status_chunk_end = None
        self.status_mosaic_start = None
        self.status_mosaic_end = None
        self.status_failed = None
        self.status_completed = None
        self.status_heartbeat = None

        self.status_validate_error = None
        self.status_thumbnail_error = None
        self.status_create_cluster_error = None
        self.status_chunk_error = None
        self.status_mosaic_error = None
        self.status_failed_error = None
        self.save()

        for image in self.layer_images.all():
            image.reset()

    def get_absolute_url(self):
        kwargs = {
            'layer_id': self.id,
            'username': self.user.username,
        }
        return reverse('layer_detail', kwargs=kwargs)

    def get_meta_url(self):
        kwargs = {
            'layer_id': self.id,
            'username': self.user.username,
        }
        return reverse('layer_meta', kwargs=kwargs)

    def get_favorite_url(self):
        kwargs = {
            'layer_id': self.id,
        }
        return reverse('create_or_destroy_favorite', kwargs=kwargs)

    def get_retry_url(self):
        return reverse('layer_retry')

    def get_dismiss_url(self):
        return reverse('layer_dismiss')

    def get_tile_url(self):
        url = 'https://s3.amazonaws.com/%s/%d/{z}/{x}/{y}.png'
        return url % (settings.AWS_TILES_BUCKET, self.id)

    def get_tile_bucket_path(self):
        return 's3://{}/{}'.format(settings.AWS_TILES_BUCKET, self.id)

    def process_failed_heartbeat(self):
        self.status_heartbeat = datetime.now()
        self.save()

    def update_status_start(self, status):
        value = datetime.now()
        if status == enums.STATUS_VALIDATE:
            self.status_validate_start = (value
                                          if self.status_validate_start is None
                                          else self.status_validate_start)
        elif status == enums.STATUS_THUMBNAIL:
            self.status_thumbnail_start = (value if
                                           self.status_thumbnail_start is None
                                           else self.status_thumbnail_start)
        elif status == enums.STATUS_CREATE_CLUSTER:
            self.status_create_cluster_start = \
                (value if self.status_create_cluster_start is None else
                 self.status_create_cluster_start)
        elif status == enums.STATUS_CHUNK:
            self.status_chunk_start = (value if self.status_chunk_start is None
                                       else self.status_chunk_start)
        elif status == enums.STATUS_MOSAIC:
            self.status_mosaic_start = (value
                                        if self.status_mosaic_start is None
                                        else self.status_mosaic_start)

    def update_status_end(self, status, error_message=None):
        value = datetime.now()
        if status == enums.STATUS_VALIDATE:
            self.status_validate_end = value
            self.status_validate_error = error_message
        elif status == enums.STATUS_THUMBNAIL:
            self.status_thumbnail_end = value
            self.status_thumbnail_error = error_message
        elif status == enums.STATUS_CREATE_CLUSTER:
            self.status_create_cluster_end = value
            self.status_create_cluster_error = error_message
        elif status == enums.STATUS_CHUNK:
            self.status_chunk_end = value
            self.status_chunk_error = error_message
        elif status == enums.STATUS_MOSAIC:
            self.status_mosaic_end = value
            self.status_mosaic_error = error_message
        elif status == enums.STATUS_COMPLETED:
            if error_message is not None:
                raise StatusMismatchError(
                    'Completed status does not accept errors.')
            self.status_completed = value
            # To be safe, unset the failed status and error.
            self.status_failed = None
            self.status_failed_error = None
        elif status == enums.STATUS_FAILED:
            if self.status_completed is not None:
                raise StatusMismatchError(
                    'Cannot mark completed layer as failed.')
            self.status_failed_error = error_message
        # If we had any error message mark the generic failed field.
        if error_message is not None:
            if self.status_completed is not None:
                raise StatusMismatchError(
                    'Cannot set errors on completed layer.')
            self.status_failed = value

    def mark_failed(self):
        value = datetime.now()
        self.status_failed = value

    def set_bounds(self, bounds):
        self.min_x = bounds[0]
        self.max_x = bounds[1]
        self.min_y = bounds[2]
        self.max_y = bounds[3]
        self.save(update_fields=['min_x', 'max_x', 'min_y', 'max_y'])

    def __unicode__(self):
        return '{0} -> {1}'.format(self.user.username, self.name)
示例#14
0
class EntityLocalizationBox(EntityLocalizationBase):
    x = FloatField()
    y = FloatField()
    width = FloatField()
    height = FloatField()
示例#15
0
class EntityLocalizationLine(EntityLocalizationBase):
    x0 = FloatField()
    y0 = FloatField()
    x1 = FloatField()
    y1 = FloatField()
示例#16
0
class EntityLocalizationDot(EntityLocalizationBase):
    x = FloatField()
    y = FloatField()
示例#17
0
class EntityMediaVideo(EntityMediaBase):
    """
    Fields:

    original: Originally uploaded file. Users cannot interact with it except
              by downloading it.

              .. deprecated :: Use media_files object

    segment_info: File for segment files to support MSE playback.

                  .. deprecated :: Use meda_files instead

    media_files: Dictionary to contain a map of all files for this media.
                 The schema looks like this:

                 .. code-block ::

                     map = {"archival": [ VIDEO_DEF, VIDEO_DEF,... ],
                            "streaming": [ VIDEO_DEF, VIDEO_DEF, ... ]}
                     video_def = {"path": <path_to_disk>,
                                  "codec": <human readable codec>,
                                  "resolution": <vertical pixel count, e.g. 720>


                                  ###################
                                  # Optional Fields #
                                  ###################

                                  # Path to the segments.json file for streaming files.
                                  # not expected/required for archival. Required for
                                  # MSE playback with seek support for streaming files.
                                  segment_info = <path_to_json>

                                  # If supplied will use this instead of currently
                                  # connected host. e.g. https://example.com
                                  "host": <host url>
                                  # If specified will be used for HTTP authorization
                                  # in the request for media. I.e. "bearer <token>"
                                  "http_auth": <http auth header>

                                  # Example mime: 'video/mp4; codecs="avc1.64001e"'
                                  # Only relevant for straming files, will assume
                                  # example above if not present.
                                  "codec-mime": <mime for MSE decode>

                                  "description": <description other than codec>}


    """
    original = FilePathField(path=settings.RAW_ROOT, null=True, blank=True)
    thumbnail = ImageField()
    thumbnail_gif = ImageField()
    num_frames = IntegerField(null=True)
    fps = FloatField(null=True)
    codec = CharField(null=True,max_length=256)
    width=IntegerField(null=True)
    height=IntegerField(null=True)
    segment_info = FilePathField(path=settings.MEDIA_ROOT, null=True,
                                 blank=True)
    media_files = JSONField(null=True, blank=True)
示例#18
0
class Media(Model):
    """
    Fields:

    original: Originally uploaded file. Users cannot interact with it except
              by downloading it.

              .. deprecated :: Use media_files object

    segment_info: File for segment files to support MSE playback.

                  .. deprecated :: Use meda_files instead

    media_files: Dictionary to contain a map of all files for this media.
                 The schema looks like this:

                 .. code-block ::

                     map = {"archival": [ VIDEO_DEF, VIDEO_DEF,... ],
                            "streaming": [ VIDEO_DEF, VIDEO_DEF, ... ],
                            <"audio": [AUDIO_DEF]>}
                     video_def = {"path": <path_to_disk>,
                                  "codec": <human readable codec>,
                                  "resolution": [<vertical pixel count, e.g. 720>, width]
                     audio_def = {"path": <path_to_disk>,
                                  "codec": <human readable codec>}


                                  ###################
                                  # Optional Fields #
                                  ###################

                                  # Path to the segments.json file for streaming files.
                                  # not expected/required for archival. Required for
                                  # MSE playback with seek support for streaming files.
                                  segment_info = <path_to_json>

                                  # If supplied will use this instead of currently
                                  # connected host. e.g. https://example.com
                                  "host": <host url>
                                  # If specified will be used for HTTP authorization
                                  # in the request for media. I.e. "bearer <token>"
                                  "http_auth": <http auth header>

                                  # Example mime: 'video/mp4; codecs="avc1.64001e"'
                                  # Only relevant for straming files, will assume
                                  # example above if not present.
                                  "codec_mime": <mime for MSE decode>

                                  "codec_description": <description other than codec>}


    """
    project = ForeignKey(Project,
                         on_delete=SET_NULL,
                         null=True,
                         blank=True,
                         db_column='project',
                         related_name='media_project')
    meta = ForeignKey(MediaType,
                      on_delete=SET_NULL,
                      null=True,
                      blank=True,
                      db_column='meta')
    """ Meta points to the defintion of the attribute field. That is
        a handful of AttributeTypes are associated to a given MediaType
        that is pointed to by this value. That set describes the `attribute`
        field of this structure. """
    attributes = JSONField(null=True, blank=True)
    """ Values of user defined attributes. """
    gid = CharField(max_length=36, null=True, blank=True)
    """ Group ID for the upload that created this media. Note we intentionally do
        not use UUIDField because this field is provided by the uploader and not
        guaranteed to be an actual UUID. """
    uid = CharField(max_length=36, null=True, blank=True)
    """ Unique ID for the upload that created this media. Note we intentionally do
        not use UUIDField because this field is provided by the uploader and not
        guaranteed to be an actual UUID. """
    created_datetime = DateTimeField(auto_now_add=True, null=True, blank=True)
    created_by = ForeignKey(User,
                            on_delete=SET_NULL,
                            null=True,
                            blank=True,
                            related_name='media_created_by',
                            db_column='created_by')
    modified_datetime = DateTimeField(auto_now=True, null=True, blank=True)
    modified_by = ForeignKey(User,
                             on_delete=SET_NULL,
                             null=True,
                             blank=True,
                             related_name='media_modified_by',
                             db_column='modified_by')
    name = CharField(max_length=256)
    md5 = SlugField(max_length=32)
    """ md5 hash of the originally uploaded file. """
    file = FileField(null=True, blank=True)
    last_edit_start = DateTimeField(null=True, blank=True)
    """ Start datetime of a session in which the media's annotations were edited.
    """
    last_edit_end = DateTimeField(null=True, blank=True)
    """ End datetime of a session in which the media's annotations were edited.
    """
    original = FilePathField(path=settings.RAW_ROOT, null=True, blank=True)
    thumbnail = ImageField(null=True, blank=True)
    thumbnail_gif = ImageField(null=True, blank=True)
    num_frames = IntegerField(null=True, blank=True)
    fps = FloatField(null=True, blank=True)
    codec = CharField(null=True, blank=True, max_length=256)
    width = IntegerField(null=True)
    height = IntegerField(null=True)
    segment_info = FilePathField(path=settings.MEDIA_ROOT,
                                 null=True,
                                 blank=True)
    media_files = JSONField(null=True, blank=True)
    recycled_from = ForeignKey(Project,
                               on_delete=SET_NULL,
                               null=True,
                               blank=True,
                               related_name='recycled_from')
示例#19
0
class Area(GeoFunc):
    output_field = FloatField()
示例#20
0
class AttributeTypeFloat(AttributeTypeBase):
    attr_name = "Float"
    dtype = "float"
    default = FloatField(null=True, blank=True)
    lower_bound = FloatField(null=True, blank=True)
    upper_bound = FloatField(null=True, blank=True)
示例#21
0
class Localization(Model):
    project = ForeignKey(Project,
                         on_delete=SET_NULL,
                         null=True,
                         blank=True,
                         db_column='project')
    meta = ForeignKey(LocalizationType,
                      on_delete=SET_NULL,
                      null=True,
                      blank=True,
                      db_column='meta')
    """ Meta points to the defintion of the attribute field. That is
        a handful of AttributeTypes are associated to a given LocalizationType
        that is pointed to by this value. That set describes the `attribute`
        field of this structure. """
    attributes = JSONField(null=True, blank=True)
    """ Values of user defined attributes. """
    created_datetime = DateTimeField(auto_now_add=True, null=True, blank=True)
    created_by = ForeignKey(User,
                            on_delete=SET_NULL,
                            null=True,
                            blank=True,
                            related_name='localization_created_by',
                            db_column='created_by')
    modified_datetime = DateTimeField(auto_now=True, null=True, blank=True)
    modified_by = ForeignKey(User,
                             on_delete=SET_NULL,
                             null=True,
                             blank=True,
                             related_name='localization_modified_by',
                             db_column='modified_by')
    user = ForeignKey(User, on_delete=PROTECT, db_column='user')
    media = ForeignKey(Media,
                       on_delete=SET_NULL,
                       null=True,
                       blank=True,
                       db_column='media')
    frame = PositiveIntegerField(null=True, blank=True)
    thumbnail_image = ForeignKey(Media,
                                 on_delete=SET_NULL,
                                 null=True,
                                 blank=True,
                                 related_name='localization_thumbnail_image',
                                 db_column='thumbnail_image')
    version = ForeignKey(Version,
                         on_delete=SET_NULL,
                         null=True,
                         blank=True,
                         db_column='version')
    modified = BooleanField(default=True, null=True, blank=True)
    """ Indicates whether an annotation is original or modified.
        null: Original upload, no modifications.
        false: Original upload, but was modified or deleted.
        true: Modified since upload or created via web interface.
    """
    x = FloatField(null=True, blank=True)
    """ Horizontal position."""
    y = FloatField(null=True, blank=True)
    """ Vertical position."""
    u = FloatField(null=True, blank=True)
    """ Horizontal vector component for lines."""
    v = FloatField(null=True, blank=True)
    """ Vertical vector component for lines. """
    width = FloatField(null=True, blank=True)
    """ Width for boxes."""
    height = FloatField(null=True, blank=True)
    """ Height for boxes."""
    parent = ForeignKey("self",
                        on_delete=SET_NULL,
                        null=True,
                        blank=True,
                        db_column='parent')
    """ Pointer to localization in which this one was generated from """