Beispiel #1
0
    def test_session_timeout(self):
        session = GeopediaSession()
        session.SESSION_DURATION = datetime.timedelta(seconds=-1)
        initial_session_id = session.session_id

        self.assertEqual(session.session_id, initial_session_id,
                         'Session should timeout and be updated')
Beispiel #2
0
 def test_false_initialization(self):
     with self.assertRaises(ValueError):
         GeopediaSession(username='******')
     with self.assertRaises(ValueError):
         GeopediaSession(password='******')
     with self.assertRaises(ValueError):
         GeopediaSession(username='******',
                         password='******',
                         password_md5='md5_encoded')
Beispiel #3
0
    def test_session_update(self):
        session = GeopediaSession()
        initial_session_id = session.session_id

        self.assertEqual(session.restart().session_id, initial_session_id,
                         'Session should be updated')

        self.assertEqual(session.user_id, 'NO_USER',
                         "Session user ID should be 'NO_USER'")
Beispiel #4
0
    def test_global_session(self):
        session1 = GeopediaSession(is_global=True)
        session2 = GeopediaSession(is_global=True)
        session3 = GeopediaSession(is_global=False)

        self.assertEqual(session1.session_id, session2.session_id,
                         'Global sessions should have the same session ID')
        self.assertNotEqual(
            session1.session_id, session3.session_id,
            'Global and local sessions should not have the same session ID')
Beispiel #5
0
    def setUpClass(cls):
        super().setUpClass()

        bbox = BBox(bbox=[(2947363, 4629723), (3007595, 4669471)],
                    crs=CRS.POP_WEB)
        bbox = bbox.transform(CRS.WGS84)
        query_filter1 = 'f12458==32632'
        query_filter2 = 'f12458==32635'

        cls.test_cases = [
            TestCaseContainer('All features',
                              GeopediaFeatureIterator(
                                  1749, gpd_session=GeopediaSession()),
                              min_features=100,
                              min_size=1609),
            TestCaseContainer('BBox filter',
                              GeopediaFeatureIterator('1749', bbox=bbox),
                              min_features=21),
            TestCaseContainer('Query Filter',
                              GeopediaFeatureIterator(
                                  'ttl1749', query_filter=query_filter1),
                              min_features=76),
            TestCaseContainer('Both filters - No data',
                              GeopediaFeatureIterator(
                                  1749, bbox=bbox, query_filter=query_filter1),
                              min_features=0),
            TestCaseContainer('Both filters - Some data',
                              GeopediaFeatureIterator(
                                  1749, bbox=bbox, query_filter=query_filter2),
                              min_features=21)
        ]
Beispiel #6
0
def get_austria_crop_geopedia_idx_to_crop_id_mapping():
    """
    Returns mapping between Geopedia's crop index and crop id for Austria.

    :return: pandas DataFrame with 'crop_geopedia_idx' and corresponding crop id
    :rtype: pandas.DataFrame
    """
    gpd_session = GeopediaSession()
    to_crop_id = list(
        GeopediaFeatureIterator(layer='2032', gpd_session=gpd_session))
    to_crop_id = [{
        'crop_geopedia_idx': code['id'],
        **code['properties']
    } for code in to_crop_id]
    to_crop_id = pd.DataFrame(to_crop_id)
    to_crop_id['crop_geopedia_idx'] = pd.to_numeric(
        to_crop_id.crop_geopedia_idx)
    to_crop_id.rename(index=str,
                      columns={"SNAR_BEZEI": "SNAR_BEZEI_NAME"},
                      inplace=True)
    to_crop_id.rename(index=str,
                      columns={"crop_geopedia_idx": "SNAR_BEZEI"},
                      inplace=True)

    return to_crop_id
Beispiel #7
0
def get_danish_crop_geopedia_idx_to_crop_id_mapping():
    """
    Returns mapping between Geopedia's crop index and crop id for Austria.

    :return: pandas DataFrame with 'crop_geopedia_idx' and corresponding crop id
    :rtype: pandas.DataFrame
    """
    gpd_session = GeopediaSession()
    to_crop_id = list(GeopediaFeatureIterator(layer='2050', gpd_session=gpd_session))
    to_crop_id = [{'crop_geopedia_idx': code['id'], **code['properties']} for code in to_crop_id]
    to_crop_id = pd.DataFrame(to_crop_id)
    to_crop_id['crop_geopedia_idx'] = pd.to_numeric(to_crop_id.crop_geopedia_idx)

    return to_crop_id
Beispiel #8
0
    def setUpClass(cls):
        super().setUpClass()

        bbox = BBox(bbox=[(13520759, 437326), (13522689, 438602)],
                    crs=CRS.POP_WEB)
        cls.image_field_name = 'Masks'

        cls.gpd_request = GeopediaImageRequest(
            layer=1749,
            bbox=bbox,
            image_field_name=cls.image_field_name,
            image_format=MimeType.PNG,
            data_folder=cls.OUTPUT_FOLDER,
            gpd_session=GeopediaSession(is_global=True))
        cls.image_list = cls.gpd_request.get_data(save_data=True)
Beispiel #9
0
 def gpd_session(self):
     """ Geopedia Session is a property which is kept alive in this class. Once session updating is fixed at
     Geopedia this will become redundant
     """
     # pylint: disable=protected-access
     if self._gpd_session is None or self._gpd_session._session_start + dt.timedelta(
             hours=1) < dt.datetime.now():
         try:
             self._gpd_session = GeopediaSession(
                 username=self.geopedia_config['user'],
                 password_md5=self.geopedia_config['md5pass'],
                 is_global=True)
             LOGGER.debug('Admin Geopedia session was created or updated')
         except Exception as ex:
             LOGGER.error('Could not create new Geopedia Session: \'%s\'!',
                          str(ex))
             raise RuntimeError('No session to Geopedia, exiting!')
     return self._gpd_session
Beispiel #10
0
def get_crop_features(table_id):
    """
    Returns DataFrame of crops for table_id from Geopedia

    :return: pandas DataFrame 
    :rtype: pandas.DataFrame
    """
    gpd_session = GeopediaSession()
    crop_iterator = GeopediaFeatureIterator(layer=table_id,
                                            gpd_session=gpd_session)
    to_crop_id = [{
        'crop_geopedia_idx': code['id'],
        **code['properties']
    } for code in crop_iterator]

    df = pd.DataFrame(to_crop_id)
    df['crop_geopedia_idx'] = pd.to_numeric(df.crop_geopedia_idx)

    return df
    def post(self):
        """ Authentication to the service
        """
        args = self.parser.parse_args()

        try:
            gpd_session = GeopediaSession(username=args[self.USERNAME],
                                          password_md5=args[self.PASSWORD])
            LOGGER.info("Created a new session for user '%s'",
                        gpd_session.username)
        except DownloadFailedException:
            return {MESSAGE: 'Invalid username or password'}, 401

        access_token = create_access_token(
            (gpd_session.session_id, gpd_session.user_id))

        # TODO: add user to users_table if not there already
        if orchestrator.is_new_user(gpd_session.user_id):
            orchestrator.add_new_user(gpd_session.username,
                                      gpd_session.user_id)

        if args[self.FORMAT] == self.JSON_FORMAT:
            return {AUTHORIZATION_STR: access_token}
        return Response(access_token, content_type=self.TEXT_FORMAT)