Exemplo n.º 1
0
def main():
    column_short = sys.argv[1]
    column_long = sys.argv[2]
    config_file = "keplergl_config.json"
    data_id = "buildings"
    config = read_configuration(config_file,
                                label="Buildings",
                                data_id=data_id,
                                color_column=column_long)
    print("Using configuration (JSON syntax):")
    print(json.dumps(config, indent=2))
    kepler = KeplerGl(config=config)
    kepler.add_data(data=open("buildings.geojson").read(), name=data_id)
    output = f"keplergl_{column_short}.html"
    kepler.save_to_html(file_name=output)

    name = column_long.replace("_", " ")

    # Add map title and creator
    with InPlace(output) as file:
        for line in file:
            line = line.replace(
                "<title>Kepler.gl</title>",
                f"<title>{name} &ndash; Campus Research Reopening Map"
                " by NCSU CGA</title>",
            )
            line = line.replace("Kepler.gl Jupyter",
                                "Kepler.gl Map by NCSU CGA")
            file.write(line)
def write_html(geojson_file, data_id, output_html, config, title):
    """Write Kepler.gl HTML and update its content"""
    # Lazy load non-standard dependencies to play nicely in cases
    # when only interface description is requested and the module
    # actually does not run.
    try:
        # pylint: disable=import-outside-toplevel
        from keplergl import KeplerGl
        from in_place import InPlace
    except ImportError as error:
        gs.fatal(
            _("Missing mandatory keplergl or in_place dependencies: {error}").
            format(error=error))

    # Useful to examine the resulting configuration
    # print("Using configuration (JSON syntax):")
    # print(json.dumps(config, indent=2))
    kepler = KeplerGl(config=config)
    kepler.add_data(data=open(geojson_file).read(), name=data_id)
    kepler.save_to_html(file_name=output_html)

    # Add map title and creator
    with InPlace(output_html) as file:
        for line in file:
            line = line.replace(
                "<title>Kepler.gl</title>",
                f"<title>{title} &ndash; GRASS GIS Kepler.gl</title>",
            )
            line = line.replace("Kepler.gl Jupyter", title)
            file.write(line)
Exemplo n.º 3
0
    def create_map(self, filename='airport_map'):
        flight_map = KeplerGl(height=500, width=800)

        flight_feature_collection = {
            'type': 'FeatureCollection',
            'features': self.flight_list
        }

        large_airport_feature_collection = {
            'type':
            'FeatureCollection',
            'features': [
                airport for airport in self.airport_list
                if airport['properties']['facility_type'] == 'large_airport'
            ]
        }

        medium_airport_feature_collection = {
            'type':
            'FeatureCollection',
            'features': [
                airport for airport in self.airport_list
                if airport['properties']['facility_type'] == 'medium_airport'
            ]
        }

        small_airport_feature_collection = {
            'type':
            'FeatureCollection',
            'features': [
                airport for airport in self.airport_list
                if airport['properties']['facility_type'] == 'small_airport'
            ]
        }

        flight_map.add_data(data=flight_feature_collection, name='flights')
        flight_map.add_data(data=large_airport_feature_collection,
                            name='large_airports')
        flight_map.add_data(data=medium_airport_feature_collection,
                            name='medium_airports')
        flight_map.add_data(data=small_airport_feature_collection,
                            name='small_airports')

        flight_map.save_to_html(file_name=f'{filename}.html')
        return flight_map
Exemplo n.º 4
0
def showTrajectoriesFromZones(origin_id, dest_id, direct):
    int_shapefile = gpd.read_file(direct + "TAZ/InternalCentroidZones.shp") # change ./.... to rootdir for DB
    ext_shapefile = gpd.read_file(direct + "TAZ/ExternalCentroidZones.shp") # change ./.... to rootdir for DB

    df = pd.read_csv("trajectories_condensed.csv")
    gdf_origins = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['Origin X'], df['Origin Y']))

    df = pd.read_csv("trajectories_condensed.csv")
    gdf_dests = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df['Dest X'], df['Dest Y']))

    int_trajectories_origins = clusterByZone(gdf_origins, int_shapefile, merge=False)
    int_trajectories_dests = clusterByZone(gdf_dests, int_shapefile, merge=False)
    
    matching_trajectories = trajectoriesFromZones(int_trajectories_origins, int_trajectories_dests, origin_id, dest_id)
    df = pd.read_csv("trajectories_condensed.csv")
    
    chosen_zones_map = KeplerGl(height=500)
    int_zones = clusterByZone(gdf_origins, int_shapefile, merge=True)
    chosen_zones_map.add_data(data=df, name='Trajectories')
    chosen_zones_map.add_data(data=matching_trajectories, name='Matching Trajectories')
    chosen_zones_map.add_data(data=int_zones, name='Internal Zones')
    
    chosen_zones_map.save_to_html(file_name="chosen_zones_map.html")
    return chosen_zones_map
Exemplo n.º 5
0
class Visualize:
    """Quickly visualize data in browser over Mapbox tiles with the help of the AMAZING kepler.gl.
    """
    def __init__(self,
                 data=None,
                 names=None,
                 read_only=False,
                 api_key=None,
                 style=None):
        """Visualize data using kepler.gl

        Args:
            data Optional[Union[List[]]]:
                either None, a List of data objects, or a single data object. If
                data is not None, then Visualize(data) will perform all steps,
                including rendering and opening a browser.
        """
        super(Visualize, self).__init__()

        if api_key is not None:
            self.MAPBOX_API_KEY = api_key
        else:
            self.MAPBOX_API_KEY = os.getenv('MAPBOX_API_KEY')
            msg = 'Warning: api_key not provided and MAPBOX_API_KEY '
            msg += 'environment variable not set.\nMap may not display.'
            if self.MAPBOX_API_KEY is None:
                print(msg)

        config = self.config(style=style)
        self.map = KeplerGl(config=config)

        if data is not None:
            self.add_data(data=data, names=names)
            self.html_path = self.render(read_only=read_only)

    def config(self, style=None):
        """Load kepler.gl config and insert Mapbox API Key"""

        config_file = resource_filename('keplergl_cli', 'keplergl_config.json')

        # First load config file as string, replace {MAPBOX_API_KEY} with the
        # actual api key, then parse as JSON
        with open(config_file) as f:
            text = f.read()

        text = text.replace('{MAPBOX_API_KEY}', self.MAPBOX_API_KEY)
        keplergl_config = json.loads(text)

        # If style_url is not None, replace existing value
        standard_styles = [
            'streets',
            'outdoors',
            'light',
            'dark',
            'satellite',
            'satellite-streets',
        ]
        if style is not None:
            style = style.lower()
            if style in standard_styles:
                # Just change the name of the mapStyle.StyleType key
                keplergl_config['config']['config']['mapStyle'][
                    'styleType'] = style
            else:
                # Add a new style with that url
                d = {
                    'accessToken': self.MAPBOX_API_KEY,
                    'custom': True,
                    'id': 'custom',
                    'label': 'Custom map style',
                    'url': style
                }
                keplergl_config['config']['config']['mapStyle']['mapStyles'][
                    'custom'] = d
                keplergl_config['config']['config']['mapStyle'][
                    'styleType'] = 'custom'

        # Remove map state in the hope that it'll auto-center based on data
        # keplergl_config['config']['config'].pop('mapState')
        return keplergl_config['config']

    def add_data(self, data, names=None):
        """Add data to kepler map

        Data should be either GeoJSON or GeoDataFrame. Kepler isn't aware of the
        geojson or shapely package, so if I supply an object from one of these
        libraries, first convert it to a GeoJSON dict.
        """
        # Make `data` iterable
        if not isinstance(data, list):
            data = [data]

        # Make `names` iterable and of the same length as `data`
        if isinstance(names, list):
            # Already iterable, make sure they're the same length
            msg = 'data and names are iterables of different length'
            assert len(data) == len(names), msg
        else:
            # `names` not iterable, make sure it's the same length as `data`
            name_stub = 'data' if names is None else names
            names = [f'{name_stub}_{x}' for x in range(len(data))]

        for datum, name in zip(data, names):
            if any(isinstance(datum, c) for c in SHAPELY_GEOJSON_CLASSES):
                datum = dict(mapping(datum))

            self.map.add_data(data=datum, name=name)

    def render(self, open_browser=True, read_only=False):
        """Export kepler.gl map to HTML file and open in Chrome
        """
        # Generate path to a temporary file
        path = os.path.join(tempfile.mkdtemp(), 'vis.html')
        self.map.save_to_html(file_name=path, read_only=read_only)

        # Open saved HTML file in new tab in default browser
        webbrowser.open_new_tab('file://' + path)

        return path
Exemplo n.º 6
0
from keplergl import KeplerGl
import ast

with open("df_freq.pkl", "rb") as f:
    df = pickle.load(f)
    
df = df[["Latitude", "Longitude", "Ryear", "Cluster_ID", "Topic_ID", "freq_words"]]

df["Timestamp"] = df["Ryear"].apply(lambda x: datetime.timestamp(datetime(year=x, month=1, day=1)))

df = df.drop_duplicates()

cluster_topic_data = {}
clusters = np.unique(df["Cluster_ID"])
for cluster in clusters:
    df2 = df.loc[df["Cluster_ID"] == cluster]
    topicids = np.unique(df2["Topic_ID"])
    cluster_topic_data[cluster] = {}
    for topicid in topicids:
        s = f"topic_{topicid}"
        df3 = df2.loc[df2["Topic_ID"] == topicid]
        cluster_topic_data[cluster][s] = df3
     
for cluster in clusters:
    with open('./config/config_'+str(cluster)+'.txt', "r") as f:
        config = f.read()
    x = ast.literal_eval(config)
    w = KeplerGl(data=cluster_topic_data[cluster], config=x)
    file_name = f"./kepler/map_{cluster}.html"
    w.save_to_html(file_name=file_name)
from keplergl import KeplerGl 


# df needs to contain 'latitude' and 'longitude' columns
map_ = KeplerGl(height=700)
map_.add_data(df, 'Data')
map_.save_to_html(file_name='try.html')

Exemplo n.º 8
0
import pandas as pd
import os
from six.moves import urllib

import pandas as pd  # importing the Pandas Library as 'pd'
from keplergl import KeplerGl  # importing KeplerGl
import geopandas as gpd  # importing geopandas as 'gpd'

# datasheet
dataset = pd.read_csv("poluition_map.csv",
                      encoding='utf8',
                      delimiter=',',
                      engine='python')

dataset.head()

#Create a basemap
map = KeplerGl(height=600, width=800)

# Create a geodataframe
gdf = gpd.GeoDataFrame(dataset,
                       geometry=gpd.points_from_xy(dataset.latitude,
                                                   dataset.longitude))
#make sure that your latitude and longitude are named as they are in your csv

map.add_data(data=gdf, name="IQA")  # add geoenabled dataframe to map

map.save_to_html(file_name='GeoViz.html')

map
Exemplo n.º 9
0
class RastaKepler(Visualize):
    """Inheriting from the `Visualize` and adding option to pass the output
    map to save map at custom location alongisde the option to pass
    a custom config file.
    """
    def __init__(
        self,
        data=None,
        names=None,
        read_only=False,
        api_key=None,
        style=None,
        config_file=None,
        output_map=None,
    ):
        if api_key is not None:
            self.MAPBOX_API_KEY = api_key
        else:
            self.MAPBOX_API_KEY = os.getenv("MAPBOX_API_KEY")
            msg = "Warning: api_key not provided and MAPBOX_API_KEY "
            msg += "environment variable not set.\nMap may not display."
            if self.MAPBOX_API_KEY is None:
                print(msg)
        if config_file is None:
            self.config_file = resource_filename("rasta",
                                                 "keplergl_config.json")
        else:
            self.config_file = config_file
        print(self.config_file)

        if output_map is not None:
            self.path = output_map + "_vis.html"
        else:
            self.path = os.path.join(tempfile.mkdtemp(), "defaultmap_vis.html")
        config = self.config(style=style)
        self.map = KeplerGl(config=config)

        if data is not None:
            self.add_data(data=data, names=names)
            self.html_path = self.render(read_only=read_only)

    def config(self, style=None):
        """Load kepler.gl config and insert Mapbox API Key"""

        # config_file = resource_filename('keplergl_cli', 'keplergl_config.json')

        # First load config file as string, replace {MAPBOX_API_KEY} with the
        # actual api key, then parse as JSON
        with open(self.config_file) as f:
            text = f.read()

        text = text.replace("{MAPBOX_API_KEY}", self.MAPBOX_API_KEY)
        keplergl_config = json.loads(text)

        # If style_url is not None, replace existing value
        standard_styles = [
            "streets",
            "outdoors",
            "light",
            "dark",
            "satellite",
            "satellite-streets",
        ]
        if style is not None:
            style = style.lower()
            if style in standard_styles:
                # Just change the name of the mapStyle.StyleType key
                keplergl_config["config"]["config"]["mapStyle"][
                    "styleType"] = style
            else:
                # Add a new style with that url
                d = {
                    "accessToken": self.MAPBOX_API_KEY,
                    "custom": True,
                    "id": "custom",
                    "label": "Custom map style",
                    "url": style,
                }
                keplergl_config["config"]["config"]["mapStyle"]["mapStyles"][
                    "custom"] = d
                keplergl_config["config"]["config"]["mapStyle"][
                    "styleType"] = "custom"

        # Remove map state in the hope that it'll auto-center based on data
        # keplergl_config['config']['config'].pop('mapState')
        return keplergl_config["config"]

    def render(self, open_browser=True, read_only=False):
        """Export kepler.gl map to HTML file and open in Chrome"""
        self.map.save_to_html(file_name=self.path, read_only=read_only)
        # Open saved HTML file in new tab in default browser
        if open_browser:
            webbrowser.open_new_tab("file://" + self.path)
        return self.path
Exemplo n.º 10
0
def generate_vis():
  config = {
    "version": "v1",
    "config": {
      "visState": {
        "filters": [],
        "layers": [
          {
            "id": "rfnq31gn",
            "type": "grid",
            "config": {
              "dataId": "income",
              "label": "Point",
              "color": [
                255,
                203,
                153
              ],
              "columns": {
                "lat": "Lat",
                "lng": "Lon"
              },
              "isVisible": True,
              "visConfig": {
                "opacity": 0.89,
                "worldUnitSize": 15,
                "colorRange": {
                  "name": "Global Warming",
                  "type": "sequential",
                  "category": "Uber",
                  "colors": [
                    "#5A1846",
                    "#900C3F",
                    "#C70039",
                    "#E3611C",
                    "#F1920E",
                    "#FFC300"
                  ]
                },
                "coverage": 1,
                "sizeRange": [
                  0,
                  1000
                ],
                "percentile": [
                  0,
                  100
                ],
                "elevationPercentile": [
                  0,
                  100
                ],
                "elevationScale": 5,
                "colorAggregation": "average",
                "sizeAggregation": "average",
                "enable3d": True
              },
              "hidden": False,
              "textLabel": [
                {
                  "field": None,
                  "color": [
                    255,
                    255,
                    255
                  ],
                  "size": 18,
                  "offset": [
                    0,
                    0
                  ],
                  "anchor": "start",
                  "alignment": "center"
                }
              ]
            },
            "visualChannels": {
              "colorField": {
                "name": "Median",
                "type": "integer"
              },
              "colorScale": "quantile",
              "sizeField": {
                "name": "Median",
                "type": "integer"
              },
              "sizeScale": "linear"
            }
          }
        ],
        "interactionConfig": {
          "tooltip": {
            "fieldsToShow": {
              "97ap1ofch": [
                {
                  "name": "id",
                  "format": None
                },
                {
                  "name": "State_Code",
                  "format": None
                },
                {
                  "name": "State_Name",
                  "format": None
                },
                {
                  "name": "State_ab",
                  "format": None
                },
                {
                  "name": "County",
                  "format": None
                }
              ]
            },
            "compareMode": False,
            "compareType": "absolute",
            "enabled": True
          },
          "brush": {
            "size": 0.5,
            "enabled": False
          },
          "geocoder": {
            "enabled": False
          },
          "coordinate": {
            "enabled": False
          }
        },
        "layerBlending": "normal",
        "splitMaps": [],
        "animationConfig": {
          "currentTime": None,
          "speed": 1
        }
      },
      "mapState": {
        "bearing": 24,
        "dragRotate": True,
        "latitude": 30.067203168518454,
        "longitude": -128.1037388392268,
        "pitch": 50,
        "zoom": 3.379836309981588,
        "isSplit": False
      },
      "mapStyle": {
        "styleType": "dark",
        "topLayerGroups": {},
        "visibleLayerGroups": {
          "label": True,
          "road": True,
          "border": False,
          "building": True,
          "water": True,
          "land": True,
          "3d building": False
        },
        "threeDBuildingColor": [
          9.665468314072013,
          17.18305478057247,
          31.1442867897876
        ],
        "mapStyles": {}
      }
    }
  }

  m = KeplerGl()
  m.config = config
  m.add_data(data=import_data(), name="income")
  m.save_to_html(file_name="index.html")
Exemplo n.º 11
0
                    "pk.eyJ1IjoianVzdHN0YW4iLCJhIjoiY2tlc3hncWJ4MWh3cTJ4cXY5ZmsxdmoyYiJ9.LHycdQ3Il_MDxeW8JfiNWw",
                    "custom": True,
                    "icon":
                    "https://api.mapbox.com/styles/v1/mapbox/satellite-streets-v11/static/-122.3391,37.7922,9,0,0/400x300?access_token=pk.eyJ1IjoianVzdHN0YW4iLCJhIjoiY2tlc3hncWJ4MWh3cTJ4cXY5ZmsxdmoyYiJ9.LHycdQ3Il_MDxeW8JfiNWw&logo=false&attribution=false",
                    "id": "satellite-streets",
                    "label": "Mapbox Satellite Streets",
                    "url": "mapbox://styles/mapbox/satellite-streets-v11"
                }
            }
        }
    }
}
## 1st
df = pd.read_csv("data/olist_orders_geo.csv")

map_1 = KeplerGl(height=600, width=800, data={"data_1": df}, config=config)
map_1.add_data(data=df, name='data_1')
map_1.data
print(df.head())

gdf = gpd.GeoDataFrame(df,
                       geometry=gpd.points_from_xy(df.cust_lng, df.cust_lat))

viz = Visualize(gdf, api_key=MAPBOX_API_KEY)

viz.render(open_browser=True, read_only=False)

map_1.save_to_html(data={'data_1': df},
                   config=config,
                   file_name='olist-order-geo-config.html')
Exemplo n.º 12
0
# + colab={"base_uri": "https://localhost:8080/", "height": 0} colab_type="code" executionInfo={"elapsed": 32915, "status": "ok", "timestamp": 1574653714934, "user": {"displayName": "So Negishi", "photoUrl": "", "userId": "14957100293746809516"}, "user_tz": 300} id="tS9Pv_2K5-LV" outputId="35d38b54-d0b7-4ec0-8e82-56b8bb1df5a0"
airport_map = KeplerGl(height=900, width=800)
airport_map_data = airport_df[[
    'name', 'latitude', 'longitude', 'facility_type'
]]
airport_map.add_data(
    data=airport_map_data[airport_map_data.facility_type == 'large_airport'],
    name='large_airports')
airport_map.add_data(
    data=airport_map_data[airport_map_data.facility_type == 'medium_airport'],
    name='medium_airports')
airport_map.add_data(
    data=airport_map_data[airport_map_data.facility_type == 'small_airport'],
    name='small_airports')
airport_map.save_to_html(file_name='./visualization/airport_map.html')
# airport_map

# + [markdown] colab_type="text" id="uJf8Ce225-LY"
# ## 2. Get flight info

# + colab={"base_uri": "https://localhost:8080/", "height": 0} colab_type="code" executionInfo={"elapsed": 39951, "status": "ok", "timestamp": 1574653721974, "user": {"displayName": "So Negishi", "photoUrl": "", "userId": "14957100293746809516"}, "user_tz": 300} id="IN6ZY7qS5-LY" outputId="9b70a9cf-0857-4370-f401-d81fc28e2bbb"
flight_df = pd.read_csv('./raw_data/266694930_T_ONTIME_REPORTING.csv',
                        header=0)
flight_df.columns = [col.lower() for col in flight_df.columns.tolist()]

# + colab={"base_uri": "https://localhost:8080/", "height": 0} colab_type="code" executionInfo={"elapsed": 41216, "status": "ok", "timestamp": 1574653723243, "user": {"displayName": "So Negishi", "photoUrl": "", "userId": "14957100293746809516"}, "user_tz": 300} id="y-u1C4OX5-La" outputId="88c5f28a-777f-4998-8550-92ff5234d177"
flight_df

# + colab={} colab_type="code" id="lW8_yzoW5-Lc"
flights = flight_df[['origin', 'dest']].to_records(index=False).tolist()
# mape = KeplerGl(height=500)

#

# In[23]:

map

# In[25]:

config = map.config

# In[29]:

map_2 = KeplerGl(height=800, data={"test": df})
map_2

# In[31]:

config_2 = map_2.config
config_2

# In[32]:

map_2.save_to_html(data={"test": df},
                   config=config_2,
                   file_name="Kepler_Baumkataster_Height.html")

# In[ ]:
Exemplo n.º 14
0
import config
import configparser
import pandas as pd  #importing the Pandas Library as 'pd'
from keplergl_cli import Visualize  #importing KeplerGl
from keplergl import KeplerGl
import geopandas as gpd  #importing geopandas as 'gpd'

#add mapbox API key
MAPBOX_API_KEY = config.MAPBOX_API

df = pd.read_csv("data/olist_orders_geo.csv")
print(df.head())

map_1 = KeplerGl(height=600, width=800)
map_1.add_data(data=df, name='data_1')
map_1.data
print(df.head())

gdf = gpd.GeoDataFrame(df,
                       geometry=gpd.points_from_xy(df.cust_lng, df.cust_lat))

viz = Visualize(gdf, api_key=MAPBOX_API_KEY)

viz.render(open_browser=True, read_only=False)

map_1.save_to_html(data={'data_1': df}, file_name='olist-order-geo.html')
Exemplo n.º 15
0
        sign.append(0)

df['Sign'] = sign

latitude_layer = []
longitude_layer = []
latitude = 41.377491
longitude = 64.585262
for i in range(len(df)):
    if (df['Sign'][i] == 1):
        latitude_layer.append(latitude)
        longitude_layer.append(longitude)
    else:
        latitude_layer.append(df['Latitude'][i])
        longitude_layer.append(df['Longitude'][i])

df['Latitude_Layer'] = latitude_layer
df['Longitude_Layer'] = longitude_layer

from keplergl import KeplerGl
import geopandas as gpd
map_1 = KeplerGl(height=500)
#df=pd.read_csv('df2001.csv')
df['Longitude'] = pd.to_numeric(df['Longitude'])
df['Latitude'] = pd.to_numeric(df['Latitude'])
gdf = gpd.GeoDataFrame(df,
                       geometry=gpd.points_from_xy(df.Longitude, df.Latitude))
map_1.add_data(data=gdf)

map_1.save_to_html(file_name='dark2009.html')