def fill_mapping_with_closest_locations(augmented_datasource_model):
    for extra_graph_line in (
        augmented_datasource_model.extragraphline_set.all()):
        if not extra_graph_line.identifier_mapping:
            # Skip
            continue

        datasource_to = datasource.get_datasource_by_layer(
            extra_graph_line.layer_to_add_line_to)
        datasource_from = datasource.get_datasource_by_layer(
            extra_graph_line.layer_to_get_line_from)

        location_dict_to = dict(
            (l.identifier,
             coordinates.wgs84_to_rd(l.latitude, l.longitude))
            for l in datasource_to.locations())

        location_dict_from = dict(
            (l.identifier,
             coordinates.wgs84_to_rd(l.latitude, l.longitude))
            for l in datasource_from.locations())

        # To add data FROM layer X to another layer Y, we need to be
        # able to translate identifiers FROM layer Y TO layer X. So
        # it's right that from and to are reversed.
        extra_graph_line.identifier_mapping.create_proximity_map(
            identifiers_from=location_dict_to,
            identifiers_to=location_dict_from,
            max_distance=extra_graph_line.max_distance_for_mapping)
    def parse(self, check_only=False):
        if not isinstance(self.file_object, ImageFile):
            return UnSuccessfulParserResult()

        try:
            measurement_type = MeasurementType.objects.get(project=self.project, mtype__slug="foto")
        except MeasurementType.DoesNotExist:
            return self.error("no_mtype")

        # Uniek_id: Part of the filename before the extension, in
        # upper case.
        uniek_id = os.path.splitext(self.file_object.name)[0].upper()

        try:
            location = Location.objects.get(location_code=uniek_id, project=self.project)
        except Location.DoesNotExist:
            return self.error(uniek_id)

        exif_data = get_exif_data(self.file_object)
        lat, lon = get_lat_lon(exif_data)

        if not lat or not lon:
            return self.error("gps")

        x, y = wgs84_to_rd(lon, lat)
        x0, y0 = location.the_geom.x, location.the_geom.y
        d = sqrt((x - x0) ** 2 + (y - y0) ** 2)

        if d > 75.0:
            return self.error("toofar", d)

        try:
            scheduled_measurement = ScheduledMeasurement.objects.get(
                project=self.project, contractor=self.contractor, location=location, measurement_type=measurement_type
            )
        except ScheduledMeasurement.DoesNotExist:
            return self.error("scheduled", id, str(measurement_type))

        if not check_only:
            m, _ = Measurement.objects.get_or_create(scheduled=scheduled_measurement)

            m.data = {}
            m.date = None
            m.the_geom = Point(x, y, srid=SRID)
            m.save()

            scheduled_measurement.complete = True
            scheduled_measurement.save()
            measurements = (m,)
        else:
            measurements = ()

        return SuccessfulParserResult(measurements)
def coordinate_to_composite_pixel(lon, lat):
    """Takes a (lon, lat) coordinate and figures out in which grid
    coordinates of the composite the point falls."""

    rd_x, rd_y = coordinates.wgs84_to_rd(lon, lat)

    minx, maxx, maxy, miny = settings.COMPOSITE_EXTENT
    cellsize_x, cellsize_y = settings.COMPOSITE_CELLSIZE

    cells_y = (maxy - miny + 1) / cellsize_y

    if not (minx <= rd_x <= maxx) or not (miny <= rd_y <= maxy):
        return None

    x = int((rd_x - minx) / cellsize_x)
    y = int(cells_y - ((rd_y - miny) / cellsize_y))

    return (x, y)
def coordinate_to_composite_pixel(lon, lat):

    """Takes a (lon, lat) coordinate and figures out in which grid
    coordinates of the composite the point falls."""

    rd_x, rd_y = coordinates.wgs84_to_rd(lon, lat)

    minx, maxx, maxy, miny = settings.COMPOSITE_EXTENT
    cellsize_x, cellsize_y = settings.COMPOSITE_CELLSIZE

    cells_y = (maxy - miny + 1) / cellsize_y

    if not (minx <= rd_x <= maxx) or not (miny <= rd_y <= maxy):
        return None

    x = int((rd_x - minx) / cellsize_x)
    y = int(cells_y - ((rd_y - miny) / cellsize_y))

    return (x, y)
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        post = request.POST
        message_list = ["result: ", ]
        try:
            google_x = float(post['google_x'])
            google_y = float(post['google_y'])
            c_rd = coordinates.google_to_rd(google_x, google_y)
            message_list.append('Google (%s, %s) = RD (%s, %s)' % (
                    google_x, google_y, c_rd[0], c_rd[1]))
            c_wgs84 = coordinates.google_to_wgs84(google_x, google_y)
            message_list.append('Google (%s, %s) = WGS84 (%s, %s)' % (
                    google_x, google_y, c_wgs84[0], c_wgs84[1]))
        except:
            pass

        try:
            rd_x = float(post['rd_x'])
            rd_y = float(post['rd_y'])
            c_google = coordinates.rd_to_google(rd_x, rd_y)
            message_list.append('RD (%s, %s) = Google (%s, %s)' % (
                    rd_x, rd_y, c_google[0], c_google[1]))
            c_wgs84 = coordinates.rd_to_wgs84(rd_x, rd_y)
            message_list.append('RD (%s, %s) = WGS84 (%s, %s)' % (
                    rd_x, rd_y, c_wgs84[0], c_wgs84[1]))
        except:
            pass

        try:
            wgs84_x = float(post['wgs84_x'])
            wgs84_y = float(post['wgs84_y'])
            c_google = coordinates.wgs84_to_google(wgs84_x, wgs84_y)
            message_list.append('WGS84 (%s, %s) = Google (%s, %s)' % (
                    wgs84_x, wgs84_y, c_google[0], c_google[1]))
            c_rd = coordinates.wgs84_to_rd(wgs84_x, wgs84_y)
            message_list.append('WGS84 (%s, %s) = RD (%s, %s)' % (
                    wgs84_x, wgs84_y, c_rd[0], c_rd[1]))
        except:
            pass

        self.message = '<br/>'.join(message_list)
        return super(ConvertView, self).get(request, *args, **kwargs)
    def parse(self, check_only=False):
        if not isinstance(self.file_object, ImageFile):
            return UnSuccessfulParserResult()

        try:
            measurement_type = MeasurementType.objects.get(
                project=self.project,
                mtype__slug='oeverfoto')
        except MeasurementType.DoesNotExist:
            return self.error('no_mtype')

        # Filename is of the format "ID_L" or "ID_R", possibly in
        # mixed case. Since IDs are upper case, we change the filename
        # to upper case first.
        filename_no_suffix = os.path.splitext(self.file_object.name)[0].upper()

        is_left = filename_no_suffix.endswith('_L')
        is_right = filename_no_suffix.endswith('_R')
        if not (is_left or is_right):
            return self.error('notleftright')

        uniek_id = filename_no_suffix[:-2]

        try:
            location = Location.objects.get(
                location_code=uniek_id,
                project=self.project)
        except Location.DoesNotExist:
            return self.error('nolocation', uniek_id)

        exif_data = get_exif_data(self.file_object)
        lat, lon = get_lat_lon(exif_data)

        if not lat or not lon:
            return self.error('gps')

        x, y = wgs84_to_rd(lon, lat)
        x0, y0 = location.the_geom.x, location.the_geom.y
        d = sqrt((x - x0) ** 2 + (y - y0) ** 2)

        if d > 75.0:
            return self.error('toofar', d)

        try:
            scheduled_measurement = (ScheduledMeasurement.objects.
                                     get(project=self.project,
                                         contractor=self.contractor,
                                         location=location,
                                         measurement_type=measurement_type))
        except ScheduledMeasurement.DoesNotExist:
            return self.error('notscheduled', id, str(measurement_type))

        # Several measurements per scheduled measurement, find if ours exists
        # The measurements store 'left' or 'right' in the data field.
        data_field = 'left' if is_left else 'right'

        if not check_only:
            measurements = {}
            for m in scheduled_measurement.measurement_set.all():
                measurements[m.data] = m

            if data_field in measurements:
                measurement = measurements[data_field]
            else:
                # New
                measurement = Measurement(
                    scheduled=scheduled_measurement, data=data_field)
            measurements[data_field] = measurement

            measurement.date = None
            measurement.the_geom = Point(x, y, srid=SRID)
            measurement.save()

            if 'left' in measurements and 'right' in measurements:
                scheduled_measurement.complete = True
                scheduled_measurement.save()

            return self.success((measurement,))
        else:
            return self.success(())