mirror of
https://github.com/bellingcat/geoclustering.git
synced 2026-06-07 19:18:30 +03:00
feat: optional kepler.gl integration (#12)
This commit is contained in:
@@ -18,10 +18,14 @@ A cluster is created when a certain number of points (defined with `--size`) eac
|
||||
Install with pip:
|
||||
|
||||
```sh
|
||||
# with kepler.gl visualization support
|
||||
pip install geoclustering[full]
|
||||
|
||||
# only text-based output
|
||||
pip install geoclustering
|
||||
```
|
||||
|
||||
If the install fails, you might need to install kepler.gl build dependencies:
|
||||
If the `full` install fails, you might need to install kepler.gl build dependencies:
|
||||
|
||||
```sh
|
||||
# macos
|
||||
@@ -138,7 +142,7 @@ It is assumed that you are using **Python3.9+**. It is encouraged to [setup a vi
|
||||
|
||||
```sh
|
||||
# install dependencies & dev-dependencies
|
||||
pip install -e .[dev]
|
||||
pip install -e .[dev,full]
|
||||
|
||||
# install a git hook that runs the code formatter before each commit.
|
||||
pre-commit install
|
||||
|
||||
@@ -72,12 +72,22 @@ def main(distance, size, output, filename, algorithm, open, debug):
|
||||
io.write_output_file(output, "result.txt", encoded["string"])
|
||||
io.write_output_file(output, "result.json", encoded["json"])
|
||||
io.write_output_file(output, "result.geojson", encoded["geojson"])
|
||||
|
||||
vis = io.write_visualization(output, "result.html", encoded["geojson"])
|
||||
if vis is None:
|
||||
print_debug(f"Skipped generating visualization: kepler is not installed.")
|
||||
|
||||
click.echo(f"Output files saved to {Path(output).absolute()}")
|
||||
|
||||
if open:
|
||||
print_debug(f"Opening visualization in default browser")
|
||||
if vis:
|
||||
webbrowser.open_new_tab("file://" + str(vis.absolute()))
|
||||
print_debug(f"Opened visualization in default browser.")
|
||||
else:
|
||||
click.secho(
|
||||
"Can't open kepler.gl: package not installed. Please re-install geoclustering with `pip install geoclustering[full]`.",
|
||||
fg="yellow",
|
||||
)
|
||||
|
||||
click.secho("Clustering completed.", fg="green")
|
||||
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import math
|
||||
from keplergl import KeplerGl
|
||||
from pathlib import Path
|
||||
from pkg_resources import resource_filename
|
||||
import json
|
||||
@@ -8,6 +6,14 @@ import numpy as np
|
||||
import os
|
||||
import sys
|
||||
|
||||
# kepler is optional, check if installed.
|
||||
try:
|
||||
from keplergl import KeplerGl
|
||||
except:
|
||||
has_kepler = False
|
||||
else:
|
||||
has_kepler = True
|
||||
|
||||
|
||||
class HiddenPrints:
|
||||
"""Disables stdout prints for a block of code."""
|
||||
@@ -87,6 +93,10 @@ def write_output_file(dirname, filename, data):
|
||||
|
||||
def write_visualization(dirname, filename, data):
|
||||
"""Write a visualization, ensuring parent directories."""
|
||||
|
||||
if not has_kepler:
|
||||
return None
|
||||
|
||||
# Hide kepler stdout output.
|
||||
with HiddenPrints():
|
||||
map = KeplerGl()
|
||||
|
||||
Reference in New Issue
Block a user