예제 #1
0
 def test__init_maps_size(self):
     """Layout should init properly"""
     layout = Layout(
         [Map(Layer(Source(SOURCE))),
          Map(Layer(Source(SOURCE)))], 1, 2)
     assert layout._n_size == 1
     assert layout._m_size == 2
예제 #2
0
 def test_custom_carto_vl_path(self):
     """Map dev path should use custom paths"""
     _carto_vl_path = 'custom_carto_vl_path'
     map = Map(_carto_vl_path=_carto_vl_path)
     map._repr_html_()
     template = map._html_map.html
     assert _carto_vl_path + constants.CARTO_VL_DEV in template
예제 #3
0
    def test_map_publish_with_password(self, mocker):
        setup_mocks(mocker)

        map = Map(Layer(Source('fake_table', credentials=self.credentials)))

        name = 'cf_publish'
        kuviz_dict = map.publish(name, '1234', credentials=self.credentials)
        self.assert_kuviz_dict(kuviz_dict, name, 'password')
예제 #4
0
    def test_is_static(self):
        """Layout should set correctly is_static property for each map"""
        layout = Layout([
            Map(Layer(Source(SOURCE))),
            Map(Layer(Source(SOURCE)))
        ], viewport={'zoom': 5})

        assert layout._layout[0].get('is_static') is True
        assert layout._layout[1].get('is_static') is True
예제 #5
0
    def test_custom_viewport(self):
        """Layout should return a different viewport for every map"""
        layout = Layout([
            Map(Layer(Source(SOURCE)), viewport={'zoom': 2}),
            Map(Layer(Source(SOURCE)))
        ], viewport={'zoom': 5})

        assert layout._layout[0].get('viewport') == {'zoom': 2}
        assert layout._layout[1].get('viewport') == {'zoom': 5}
예제 #6
0
    def test_global_viewport(self):
        """Layout should return the same viewport for every map"""
        layout = Layout([
            Map(Layer(Source(SOURCE))),
            Map(Layer(Source(SOURCE)))
        ], viewport={'zoom': 5})

        assert layout._layout[0].get('viewport') == {'zoom': 5}
        assert layout._layout[1].get('viewport') == {'zoom': 5}
예제 #7
0
    def test_map_publish_deletion(self, mocker):
        setup_mocks(mocker)

        map = Map(Layer(Source('fake_table', credentials=self.credentials)))

        name = 'cf_publish'
        map.publish(name, None, credentials=self.credentials)
        response = map.delete_publication()

        assert response is True
예제 #8
0
    def test_map_publish_update_password(self, mocker):
        setup_mocks(mocker)

        map = Map(Layer(Source('fake_table', credentials=self.credentials)))

        name = 'cf_publish'
        map.publish(name, None, credentials=self.credentials)
        kuviz_dict = map.update_publication(name, '1234"')

        self.assert_kuviz_dict(kuviz_dict, name, 'password')
예제 #9
0
 def test_default_airship_path(self):
     """Map dev path should use default paths if none are given"""
     map = Map()
     map._repr_html_()
     template = map._html_map.html
     assert constants.AIRSHIP_COMPONENTS_URL in template
     assert constants.AIRSHIP_BRIDGE_URL in template
     assert constants.AIRSHIP_STYLES_URL in template
     assert constants.AIRSHIP_MODULE_URL in template
     assert constants.AIRSHIP_ICONS_URL in template
예제 #10
0
    def test_map_publish_remote_default(self, mocker):
        setup_mocks(mocker)
        mock_set_content = mocker.patch(
            'cartoframes.viz.html.html_map.HTMLMap.set_content')

        vmap = Map(Layer('fake_table', credentials=self.credentials))

        name = 'cf_publish'
        kuviz_dict = vmap.publish(name, None, self.credentials)
        self.assert_kuviz_dict(kuviz_dict, name, 'public')
        mock_set_content.assert_called_once_with(
            _airship_path=None,
            _carto_vl_path=None,
            basemap='Positron',
            bounds=[[-180, -90], [180, 90]],
            camera=None,
            description=None,
            is_embed=True,
            is_static=None,
            layer_selector=False,
            layers=[{
                'credentials': {
                    'username': '******',
                    'api_key': 'fake_api_key',
                    'base_url': 'https://fake_username.carto.com'
                },
                'interactivity': [],
                'legends': [],
                'has_legend_list':
                True,
                'encode_data':
                True,
                'widgets': [],
                'data':
                'select * from fake_table',
                'type':
                'Query',
                'title':
                None,
                'options': {},
                'map_index':
                0,
                'source':
                'select * from fake_table',
                'viz':
                '''color: hex("#EE4D5A")
strokeColor: opacity(#222,ramp(linear(zoom(),0,18),[0,0.6]))
strokeWidth: ramp(linear(zoom(),0,18),[0,1])
width: ramp(linear(zoom(),0,18),[2,10])
'''
            }],
            show_info=False,
            size=None,
            theme=None,
            title='cf_publish')
예제 #11
0
 def test_custom_airship_path(self):
     """Map dev path should use custom paths"""
     _airship_path = 'custom_airship_path'
     map = Map(_airship_path=_airship_path)
     map._repr_html_()
     template = map._html_map.html
     assert _airship_path + constants.AIRSHIP_COMPONENTS_DEV in template
     assert _airship_path + constants.AIRSHIP_BRIDGE_DEV in template
     assert _airship_path + constants.AIRSHIP_STYLES_DEV in template
     assert _airship_path + constants.AIRSHIP_MODULE_DEV in template
     assert _airship_path + constants.AIRSHIP_ICONS_DEV in template
예제 #12
0
    def test_custom_camera(self):
        """Layout should return the correct camera for each map"""
        layout = Layout([
            Map(Layer(Source(SOURCE)), viewport={'zoom': 2}),
            Map(Layer(Source(SOURCE)))
        ], viewport={'zoom': 5})

        assert layout._layout[0].get('camera') == {
            'bearing': None, 'center': None, 'pitch': None, 'zoom': 2}
        assert layout._layout[1].get('camera') == {
            'bearing': None, 'center': None, 'pitch': None, 'zoom': 5}
예제 #13
0
    def test_default_interactive_layer(self):
        """Map layer should get the default event if the interactivity is set to []"""
        source_1 = Source(build_geodataframe([-10, 0], [-10, 0]))
        layer = Layer(source_1)

        map = Map(layer)
        assert map.layer_defs[0].get('interactivity') == []
예제 #14
0
    def test_interactive_layer(self):
        """Map layer should indicate if the layer has interactivity configured"""
        source_1 = Source(
            build_geodataframe([-10, 0], [-10, 0], ['pop', 'name']))
        layer = Layer(
            source_1,
            popup_click=[popup_element('pop'),
                         popup_element('name')],
            popup_hover=[popup_element('pop', 'Pop')])

        map = Map(layer)
        layer_def = map.layers[0].get_layer_def()
        assert layer_def.get('interactivity') == [{
            'event': 'click',
            'attrs': {
                'name': 'v6ae999',
                'title': 'name',
                'format': None
            }
        }, {
            'event': 'click',
            'attrs': {
                'name': 'v4f197c',
                'title': 'pop',
                'format': None
            }
        }, {
            'event': 'hover',
            'attrs': {
                'name': 'v4f197c',
                'title': 'Pop',
                'format': None
            }
        }]
예제 #15
0
    def test_default_interactive_layer(self):
        """Map layer should get the default event if the interactivity is set to []"""
        source_1 = Source(build_geojson([-10, 0], [-10, 0]))
        layer = Layer(source_1, popup={})

        map = Map(layer)
        self.assertEqual(map.layer_defs[0].get('interactivity'), [])
예제 #16
0
    def test_interactive_layer(self):
        """Map layer should indicate if the layer has interactivity configured"""
        source_1 = Source(build_geojson([-10, 0], [-10, 0]))
        layer = Layer(source_1,
                      popup={
                          'click': ['$pop', '$name'],
                          'hover': [{
                              'title': 'Pop',
                              'value': '$pop'
                          }]
                      })

        map = Map(layer)
        self.assertEqual(map.layer_defs[0].get('interactivity'),
                         [{
                             'event':
                             'click',
                             'attrs': [{
                                 'name': 'v559339',
                                 'title': '$pop'
                             }, {
                                 'name': 'v8e0f74',
                                 'title': '$name'
                             }]
                         }, {
                             'event': 'hover',
                             'attrs': [{
                                 'name': 'v559339',
                                 'title': 'Pop'
                             }]
                         }])
예제 #17
0
    def test_kuviz_delete_publication(self, mocker):
        setup_mocks(mocker, self.credentials)

        html = 'fake_html'
        kuviz_name = 'fake_name'
        kuviz = CartoKuvizMock(kuviz_name)

        KuvizManagerNamedtuple = namedtuple('KuvizManager', ['all'])
        kuviz_manager_namedtuple = KuvizManagerNamedtuple(lambda: [kuviz])

        mocker.patch('cartoframes.viz.kuviz._create_kuviz', return_value=kuviz)
        mocker.patch('cartoframes.viz.kuviz._get_kuviz_manager',
                     return_value=kuviz_manager_namedtuple)
        mock = mocker.patch(
            'tests.unit.mocks.kuviz_mock.CartoKuvizMock.delete')

        vmap = Map(Layer('fake_table', credentials=self.credentials))

        kuviz_publisher = KuvizPublisher(None)
        kuviz_publisher.set_layers(vmap.layers)
        kuviz_publisher.publish(html, kuviz_name, None)

        delete_publication(kuviz_name, credentials=self.credentials)

        mock.assert_called_once_with()
예제 #18
0
    def test_interactive_layer(self):
        """Map layer should indicate if the layer has interactivity configured"""
        source_1 = Source(
            build_geodataframe([-10, 0], [-10, 0], ['pop', 'name']))
        layer = Layer(
            source_1,
            popup_click=[popup_element('pop'),
                         popup_element('name')],
            popup_hover=[popup_element('pop', 'Pop')])

        map = Map(layer)
        assert map.layer_defs[0].get('interactivity') == [{
            'event': 'click',
            'attrs': {
                'name': 'v8e0f74',
                'title': 'name'
            }
        }, {
            'event': 'click',
            'attrs': {
                'name': 'v559339',
                'title': 'pop'
            }
        }, {
            'event': 'hover',
            'attrs': {
                'name': 'v559339',
                'title': 'Pop'
            }
        }]
예제 #19
0
    def test_layout_publish_remote_default(self, mocker):
        setup_mocks(mocker)
        mock_set_content = mocker.patch('cartoframes.viz.html.html_layout.HTMLLayout.set_content')

        vlayout = Layout([Map(Layer('fake_table', credentials=self.credentials))])

        name = 'cf_publish'
        kuviz_dict = vlayout.publish(name, None, self.credentials)
        self.assert_kuviz_dict(kuviz_dict, name, 'public')
        mock_set_content.assert_called_once_with(
            is_embed=True,
            is_static=False,
            m_size=1,
            n_size=1,
            size=['100%', 250],
            map_height='100%',
            maps=[{
                'layers': [{
                    'credentials': {
                        'username': '******',
                        'api_key': 'fake_api_key',
                        'base_url': 'https://fake_username.carto.com'
                    },
                    'interactivity': [],
                    'legends': [],
                    'has_legend_list': True,
                    'encode_data': True,
                    'widgets': [],
                    'data': 'select * from fake_table',
                    'type': 'Query',
                    'title': None,
                    'options': {},
                    'map_index': 0,
                    'source': 'select * from fake_table',
                    'viz': '''color: hex("#EE4D5A")
strokeColor: opacity(#222,ramp(linear(zoom(),0,18),[0,0.6]))
strokeWidth: ramp(linear(zoom(),0,18),[0,1])
width: ramp(linear(zoom(),0,18),[2,10])
'''
                }],
                'bounds': [[-180, -90], [180, 90]],
                'size': None,
                'viewport': None,
                'camera': None,
                'basemap': 'Positron',
                'basecolor': '',
                'token': '',
                'show_info': None,
                'has_legends': False,
                'has_widgets': False,
                'theme': None,
                'title': None,
                'description': None,
                'is_static': False,
                'layer_selector': False,
                '_carto_vl_path': None,
                '_airship_path': None
            }]
        )
예제 #20
0
 def test_default_airship_path(self):
     """Map dev path should use default paths if none are given"""
     map = Map()
     template = map._htmlMap.html
     self.assertTrue(constants.AIRSHIP_COMPONENTS_URL in template)
     self.assertTrue(constants.AIRSHIP_BRIDGE_URL in template)
     self.assertTrue(constants.AIRSHIP_STYLES_URL in template)
     self.assertTrue(constants.AIRSHIP_ICONS_URL in template)
예제 #21
0
 def test_bounds_clamp(self):
     """Map should set the bounds clamped"""
     map = Map(bounds={
         'west': -1000,
         'east': 1000,
         'north': -1000,
         'south': 1000
     })
     assert map.bounds == [[-180, 90], [180, -90]]
예제 #22
0
    def test_two_layers(self):
        """Map layer should be able to initialize two layers in the correct order"""
        source_1 = Source(build_geodataframe([-10, 0], [-10, 0]))
        source_2 = Source(build_geodataframe([0, 10], [10, 0]))
        layer_1 = Layer(source_1)
        layer_2 = Layer(source_2)
        map = Map([layer_1, layer_2])

        assert map.layers == [layer_1, layer_2]
def geo_api():
    """
    Introduciendo un criterio para llamar a la API de Foursquare, obtienes un mapa con los puntos obtenidos
    
    """

    load_dotenv()
    tok1 = os.getenv("tok1")
    tok2 = os.getenv("tok2")
    url_query = 'https://api.foursquare.com/v2/venues/explore'

    criterio = input("¿Qué quieres buscar en Foursquare? \n")
    limite = input("¿Cuántas búsquedas quieres que muestre? \n")
    radio = input("¿En qué radio?(medida = metros) \n")

    params = {
        "client_id": tok1,
        "client_secret": tok2,
        "v": "20180323",
        "ll": f"60.17212, 24.94519",
        "query": f"{criterio}",
        "limit": {limite},
        "radius": {radio}
    }

    resp = requests.get(url=url_query, params=params)
    data = json.loads(resp.text)
    decoding_data = data.get("response")
    decoded = decoding_data.get("groups")[0]
    items = decoded.get("items")

    mapa_nombre = ["venue", "name"]
    m_latitude = ["venue", "location", "lat"]
    m_longitude = ["venue", "location", "lng"]

    def getFromDict(diccionario, mapa):
        return reduce(operator.getitem, mapa, diccionario)

    list_items = []
    for dic in items:
        items_dict = {}
        items_dict["name"] = getFromDict(dic, mapa_nombre)
        items_dict["latitud"] = getFromDict(dic, m_latitude)
        items_dict["longitud"] = getFromDict(dic, m_longitude)

        list_items.append(items_dict)

        dataframefinal = pd.DataFrame(list_items)

        gdf = gpd.GeoDataFrame(dataframefinal,
                               geometry=gpd.points_from_xy(
                                   dataframefinal.longitud,
                                   dataframefinal.latitud))
        mapa = Map(
            Layer(gdf, popup_hover=[popup_element("name", f"{criterio}")]))

    return mapa
예제 #24
0
    def test_map_publish_remote_params(self, mocker):
        setup_mocks(mocker)
        mock_set_content = mocker.patch(
            'cartoframes.viz.html.html_map.HTMLMap.set_content')

        vmap = Map(Layer('fake_table', credentials=self.credentials),
                   basemap='yellow',
                   bounds={
                       'west': 1,
                       'east': 2,
                       'north': 3,
                       'south': 4
                   },
                   viewport={
                       'zoom': 5,
                       'lat': 50,
                       'lng': -10
                   },
                   is_static=True,
                   theme='dark',
                   title='title',
                   description='description')

        name = 'cf_publish'
        kuviz_dict = vmap.publish(name, None, self.credentials)
        self.assert_kuviz_dict(kuviz_dict, name, 'public')
        mock_set_content.assert_called_once_with(_airship_path=None,
                                                 _carto_vl_path=None,
                                                 basemap='yellow',
                                                 bounds=[[1, 2], [4, 3]],
                                                 camera={
                                                     'bearing': None,
                                                     'center': [-10, 50],
                                                     'pitch': None,
                                                     'zoom': 5
                                                 },
                                                 description='description',
                                                 is_embed=True,
                                                 is_static=True,
                                                 layers=[],
                                                 show_info=False,
                                                 size=None,
                                                 theme='dark',
                                                 title='cf_publish')
예제 #25
0
    def test_two_layers(self):
        """Map layer should be able to initialize two layers in the correct order"""
        source_1 = Source(build_geojson([-10, 0], [-10, 0]))
        source_2 = Source(build_geojson([0, 10], [10, 0]))
        layer_1 = Layer(source_1)
        layer_2 = Layer(source_2)
        map = Map([layer_1, layer_2])

        self.assertEqual(map.layers, [layer_2, layer_1])
        self.assertEqual(len(map.layer_defs), 2)
예제 #26
0
 def test_custom_airship_path(self):
     """Map dev path should use custom paths"""
     _airship_path = 'custom_airship_path'
     map = Map(_airship_path=_airship_path)
     template = map._htmlMap.html
     self.assertTrue(_airship_path +
                     constants.AIRSHIP_COMPONENTS_DEV in template)
     self.assertTrue(_airship_path +
                     constants.AIRSHIP_BRIDGE_DEV in template)
     self.assertTrue(_airship_path +
                     constants.AIRSHIP_STYLES_DEV in template)
     self.assertTrue(_airship_path +
                     constants.AIRSHIP_ICONS_DEV in template)
예제 #27
0
    def test_kuviz_publisher_update_fail(self, mocker):
        setup_mocks(mocker, self.credentials)

        vmap = Map(Layer('fake_table', credentials=self.credentials))

        html = 'fake_html'
        kuviz_name = 'fake_name'

        kuviz_publisher = KuvizPublisher(None)
        kuviz_publisher.set_layers(vmap.layers)

        with pytest.raises(Exception):
            kuviz_publisher.update(html, kuviz_name, None)
예제 #28
0
    def test_one_layer(self):
        """Map layer should be able to initialize one layer"""
        source = Source(build_geojson([-10, 0], [-10, 0]))
        layer = Layer(source)
        map = Map(layer)

        self.assertEqual(map.layers, [layer])
        self.assertEqual(len(map.layer_defs), 1)
        self.assertEqual(map.layer_defs[0].get('interactivity'), [])
        self.assertIsNotNone(map.layer_defs[0].get('credentials'))
        self.assertIsNotNone(map.layer_defs[0].get('legend'))
        self.assertIsNotNone(map.layer_defs[0].get('query'))
        self.assertEqual(map.layer_defs[0].get('type'), 'GeoJSON')
        self.assertIsNotNone(map.layer_defs[0].get('viz'))
예제 #29
0
    def test_kuviz_publisher_set_layers(self, mocker):
        setup_mocks(mocker, self.credentials)

        source_1 = Source(build_geodataframe([-10, 0], [-10, 0]))
        source_2 = Source(build_geodataframe([0, 10], [10, 0]))
        layer_1 = Layer(source_1)
        layer_2 = Layer(source_2)
        vmap = Map([layer_1, layer_2])

        kuviz_publisher = KuvizPublisher(None)
        kuviz_publisher.set_layers(vmap.layers)

        assert kuviz_publisher._layers != vmap.layers
        assert len(kuviz_publisher._layers) == len(vmap.layers)
예제 #30
0
    def test_one_layer(self):
        """Map layer should be able to initialize one layer"""
        source = Source(build_geodataframe([-10, 0], [-10, 0]))
        layer = Layer(source)
        map = Map(layer)

        assert map.layers == [layer]
        assert len(map.layer_defs) == 1
        assert map.layer_defs[0].get('interactivity') == []
        assert map.layer_defs[0].get('credentials') is None
        assert map.layer_defs[0].get('legends') is not None
        assert map.layer_defs[0].get('widgets') is not None
        assert map.layer_defs[0].get('data') is not None
        assert map.layer_defs[0].get('type') == 'GeoJSON'
        assert map.layer_defs[0].get('viz') is not None