Esempio n. 1
0
    def test_osm_building_contributions(self):
        """Test that we can obtain correct contribution counts for a file."""
        file_handle = open(FIXTURE_PATH)
        contributor_list = osm_object_contributions(file_handle,
                                                    tag_name="building")
        expected_list = ast.literal_eval(
            open(
                os.path.join(os.path.dirname(os.path.realpath(__file__)),
                             'test_data',
                             'expected_osm_building_contributions.txt'),
                'rb').read().decode('utf-8'))

        # noinspection PyPep8Naming
        self.maxDiff = None
        print(contributor_list)
        self.assertListEqual(contributor_list, expected_list)
Esempio n. 2
0
 def test_get_totals(self):
     """Test we get the proper totals from a sorted user list."""
     sorted_user_list = osm_object_contributions(open(FIXTURE_PATH),
                                                 tag_name="building")
     ways, nodes = get_totals(sorted_user_list)
     self.assertEquals((ways, nodes), (427, 52))
Esempio n. 3
0
    def get_data_from_provider(self):
        """ Get required attrbiutes for function provider.
        :return: dict of user list and update status
        :rtype: dict
        """
        sorted_user_list = []
        last_update = None
        is_updating = False

        if self.feature:
            start_date = calendar.timegm(
                datetime.datetime.strptime(self.campaign.start_date,
                                           '%Y-%m-%d').timetuple()) * 1000
            end_date = calendar.timegm(
                datetime.datetime.strptime(self.campaign.end_date,
                                           '%Y-%m-%d').timetuple()) * 1000

            try:
                features = self.feature.split('=')
                if len(features) == 0:
                    return []
                elif len(features) == 2:
                    feature_key = features[0]
                    feature_values = features[1].split(',')
                    overpass_data = OverpassProvider().get_attic_data(
                        polygon=self.campaign.corrected_coordinates(),
                        overpass_verbosity='meta',
                        feature_key=feature_key,
                        feature_values=feature_values,
                        date_from=str(start_date),
                        date_to=str(end_date))
                else:
                    feature_key = features[0]
                    overpass_data = OverpassProvider().get_attic_data(
                        polygon=self.campaign.corrected_coordinates(),
                        overpass_verbosity='meta',
                        feature_key=feature_key,
                        date_from=str(start_date),
                        date_to=str(end_date))
            except OverpassTimeoutException:
                error = 'Timeout, try a smaller area.'
            except OverpassBadRequestException:
                error = 'Bad request.'
            except OverpassConcurrentRequestException:
                error = 'Please try again later, another query is running.'
            except URLError:
                error = 'Bad request.'
            except OverpassDoesNotReturnData:
                error = 'No data from overpass.'
            else:
                if not overpass_data:
                    return []
                try:
                    last_update = overpass_data['last_update']
                    is_updating = overpass_data['updating_status']
                    tag_name = ''
                    if '=' in self.feature:
                        tag_name = self.feature.split('=')[0]
                    else:
                        try:
                            tag_name = TAG_MAPPING_REVERSE[self.feature]
                        except KeyError:
                            error = 'No key found'
                    if isinstance(overpass_data['file'], io.IOBase):
                        sorted_user_list = osm_object_contributions(
                            overpass_data['file'], tag_name, start_date,
                            end_date)
                except xml.sax.SAXParseException:
                    error = (
                        'Invalid OSM xml file retrieved. Please try again '
                        'later.')
            if not last_update:
                last_update = datetime.datetime.now().strftime(
                    '%Y-%m-%d %H:%M:%S')

            # Save to participant counts in file
            if self.feature_type:
                self.campaign.update_participants_count(
                    len(sorted_user_list), self.feature_type)

            return {
                'user_list': sorted_user_list,
                'last_update': last_update,
                'is_updating': is_updating
            }
        else:
            return []
Esempio n. 4
0
def home():
    """Home page view.

    On this page a map and the report will be shown.
    """
    default_tag = 'highway'
    sorted_user_list = []
    bbox = request.args.get('bbox', config.BBOX)
    tag_name = request.args.get('obj', default_tag)
    date_from = request.args.get('date_from', None)
    date_to = request.args.get('date_to', None)
    error = None
    try:
        coordinates = split_bbox(bbox)
    except ValueError:
        error = "Invalid bbox"
        coordinates = split_bbox(config.BBOX)
    else:

        if tag_name not in list(TAG_MAPPING.keys()):
            error = "Unsupported object type"
            tag_name = default_tag
        try:
            feature_type = TAG_MAPPING[tag_name]
            file_handle = get_osm_file(coordinates, feature_type, 'meta',
                                       date_from, date_to)
        except OverpassTimeoutException:
            error = 'Timeout, try a smaller area.'
        except OverpassBadRequestException:
            error = 'Bad request.'
        except OverpassConcurrentRequestException:
            error = 'Please try again later, another query is running.'
        except URLError:
            error = 'Bad request.'
        else:
            try:
                sorted_user_list = osm_object_contributions(
                    file_handle, tag_name)
            except xml.sax.SAXParseException:
                error = ('Invalid OSM xml file retrieved. Please try again '
                         'later.')

    node_count, way_count = get_totals(sorted_user_list)

    # We need to manually cast float in string, otherwise floats are
    # truncated, and then rounds in Leaflet result in a wrong bbox
    # Note: slit_bbox should better keep returning real floats
    coordinates = dict((k, repr(v)) for k, v in coordinates.items())

    download_url = '%s-shp' % TAG_MAPPING[tag_name]
    context = dict(
        sorted_user_list=sorted_user_list,
        way_count=way_count,
        node_count=node_count,
        user_count=len(sorted_user_list),
        bbox=bbox,
        current_tag_name=tag_name,
        download_url=download_url,
        available_tag_names=list(TAG_MAPPING.keys()),
        error=error,
        coordinates=coordinates,
        display_update_control=int(config.DISPLAY_UPDATE_CONTROL),
    )
    # noinspection PyUnresolvedReferences
    return render_template('base.html', **context)
Esempio n. 5
0
 def test_get_totals(self):
     """Test we get the proper totals from a sorted user list."""
     sorted_user_list = osm_object_contributions(
         osm_file=open(FIXTURE_PATH), tag_name="amenity")
     ways, nodes = get_totals(sorted_user_list)
     self.assertEquals((ways, nodes), (100, 10))