apb_spatial_utils.topojson_utils

 1#  coding=utf-8
 2#
 3#  Author: Ernesto Arredondo Martinez (ernestone@gmail.com)
 4#  Created: 7/6/19 18:23
 5#  Last modified: 7/6/19 12:42
 6#  Copyright (c) 2019
 7
 8import os
 9from apb_extra_utils import misc as utils
10
11
12def geojson_to_topojson(geojson_path, dir_topo=None, nom_layer=None, simplify=True, overwrite=True):
13    """
14    Callback a funciones de linea de comandos para conversion a Topojson.
15    Son comandos lanzados sobre nodejs.
16
17    Ver documentacion en github https://github.com/topojson/topojson-server.
18    Vease tambien tutorial de como utlizarlo en https://medium.com/@mbostock/command-line-cartography-part-3-1158e4c55a1e
19
20    Args:
21        geojson_path:
22        dir_topo:
23        nom_layer:
24        simplify:
25        overwrite:
26
27    Returns:
28        str: path del fichero generado si va bien
29    """
30    __factor_quant__ = "9e5"
31    __factor_simpl__ = "0.0000000000003"
32    dir_name, json_name = os.path.split(geojson_path)
33    base_name, ext = os.path.splitext(json_name)
34    base_name = base_name.split(".geo")[0]
35    topo_name = "{base_name}.topo{ext}".format(base_name=base_name,
36                                               ext=ext)
37    if not nom_layer:
38        nom_layer = base_name
39
40    from_path = "{nom_layer}={path}".format(nom_layer=nom_layer,
41                                            path=os.path.normpath(geojson_path))
42
43    if not dir_topo:
44        dir_topo = os.path.normpath(os.path.join(dir_name, "topojson"))
45
46    topojson_path = os.path.normpath(os.path.join(dir_topo, topo_name))
47
48    if overwrite or not os.path.exists(topojson_path):
49        if not os.path.exists(dir_topo):
50            os.makedirs(dir_topo)
51
52        params = []
53        if simplify:
54            params += ("-q", __factor_quant__)
55            t_path_aux = "{}.aux".format(topojson_path)
56            params += (from_path, ">", t_path_aux)
57            ok = utils.call_command("geo2topo", *params)
58            if ok:
59                params = []
60                params += ("-p", __factor_simpl__, "-f")
61                params += ("<", t_path_aux, ">", topojson_path)
62                ok = utils.call_command("toposimplify", *params)
63                if ok:
64                    os.remove(t_path_aux)
65        else:
66            params += (from_path, ">", topojson_path)
67            ok = utils.call_command("geo2topo", *params)
68
69        if ok:
70            return topojson_path
71
72
73def convert_geojson_files_to_topojson(dir_geojson, dir_topo, overwrite=True):
74    """
75    A partir de un directorio con geojson los convierte a Topojson
76
77    Args:
78        dir_geojson:
79        dir_topo:
80        overwrite:
81
82    Returns:
83
84    """
85    directory = os.fsencode(dir_geojson)
86
87    for file in os.listdir(directory):
88        filename = os.fsdecode(file)
89        if filename.endswith(".geo.json") or filename.endswith(".geojson"):
90            topo_file = geojson_to_topojson(
91                os.path.normpath(os.path.join(dir_geojson, filename)),
92                os.path.normpath(dir_topo), overwrite=overwrite)
93
94
95if __name__ == '__main__':
96    import fire
97    fire.Fire()
def geojson_to_topojson( geojson_path, dir_topo=None, nom_layer=None, simplify=True, overwrite=True):
13def geojson_to_topojson(geojson_path, dir_topo=None, nom_layer=None, simplify=True, overwrite=True):
14    """
15    Callback a funciones de linea de comandos para conversion a Topojson.
16    Son comandos lanzados sobre nodejs.
17
18    Ver documentacion en github https://github.com/topojson/topojson-server.
19    Vease tambien tutorial de como utlizarlo en https://medium.com/@mbostock/command-line-cartography-part-3-1158e4c55a1e
20
21    Args:
22        geojson_path:
23        dir_topo:
24        nom_layer:
25        simplify:
26        overwrite:
27
28    Returns:
29        str: path del fichero generado si va bien
30    """
31    __factor_quant__ = "9e5"
32    __factor_simpl__ = "0.0000000000003"
33    dir_name, json_name = os.path.split(geojson_path)
34    base_name, ext = os.path.splitext(json_name)
35    base_name = base_name.split(".geo")[0]
36    topo_name = "{base_name}.topo{ext}".format(base_name=base_name,
37                                               ext=ext)
38    if not nom_layer:
39        nom_layer = base_name
40
41    from_path = "{nom_layer}={path}".format(nom_layer=nom_layer,
42                                            path=os.path.normpath(geojson_path))
43
44    if not dir_topo:
45        dir_topo = os.path.normpath(os.path.join(dir_name, "topojson"))
46
47    topojson_path = os.path.normpath(os.path.join(dir_topo, topo_name))
48
49    if overwrite or not os.path.exists(topojson_path):
50        if not os.path.exists(dir_topo):
51            os.makedirs(dir_topo)
52
53        params = []
54        if simplify:
55            params += ("-q", __factor_quant__)
56            t_path_aux = "{}.aux".format(topojson_path)
57            params += (from_path, ">", t_path_aux)
58            ok = utils.call_command("geo2topo", *params)
59            if ok:
60                params = []
61                params += ("-p", __factor_simpl__, "-f")
62                params += ("<", t_path_aux, ">", topojson_path)
63                ok = utils.call_command("toposimplify", *params)
64                if ok:
65                    os.remove(t_path_aux)
66        else:
67            params += (from_path, ">", topojson_path)
68            ok = utils.call_command("geo2topo", *params)
69
70        if ok:
71            return topojson_path

Callback a funciones de linea de comandos para conversion a Topojson. Son comandos lanzados sobre nodejs.

Ver documentacion en github https://github.com/topojson/topojson-server. Vease tambien tutorial de como utlizarlo en https://medium.com/@mbostock/command-line-cartography-part-3-1158e4c55a1e

Arguments:
  • geojson_path:
  • dir_topo:
  • nom_layer:
  • simplify:
  • overwrite:
Returns:

str: path del fichero generado si va bien

def convert_geojson_files_to_topojson(dir_geojson, dir_topo, overwrite=True):
74def convert_geojson_files_to_topojson(dir_geojson, dir_topo, overwrite=True):
75    """
76    A partir de un directorio con geojson los convierte a Topojson
77
78    Args:
79        dir_geojson:
80        dir_topo:
81        overwrite:
82
83    Returns:
84
85    """
86    directory = os.fsencode(dir_geojson)
87
88    for file in os.listdir(directory):
89        filename = os.fsdecode(file)
90        if filename.endswith(".geo.json") or filename.endswith(".geojson"):
91            topo_file = geojson_to_topojson(
92                os.path.normpath(os.path.join(dir_geojson, filename)),
93                os.path.normpath(dir_topo), overwrite=overwrite)

A partir de un directorio con geojson los convierte a Topojson

Arguments:
  • dir_geojson:
  • dir_topo:
  • overwrite:

Returns: