Giter Site home page Giter Site logo

Comments (1)

mattijn avatar mattijn commented on May 28, 2024

Thanks for your words. You are right that it is currently geared towards geographical data to topojson. Better support for the other direction would be much welcome indeed.

There is already some within the code that makes this possible, but this could probably much improved. PR's definitely welcome!

Two possibilities:
First, if you parse a string into Topology(), where the string contains "type": "Topology" it is tried to serialise as geojson, and then convert to topojson again:

https://github.com/mattijn/topojson/blob/master/topojson/core/extract.py#L585

I can imagine that that is cumbersome. Based on some tests I also get strange outputs.

Second, there is another route. Parse your topojson as a dict and then use the serialise_as_geojson function:

import topojson as tp
from topojson.utils import serialize_as_geojson
import geopandas as gpd

# create a topojson dictionary
data = tp.utils.example_data_africa()
topo = tp.Topology(data)
topojson_dict = topo.to_dict()

?topojson_dict
Type:        dict
String form: {'type': 'Topology', 'objects': {'data': {'geometries': [{'id': '1', 'type': 'Polygon', 'properti <...> [750884, 541353], [-8944, -6372], [-10232, 32], [-11708, -3243], [-9248, 3099], [-5987, -3779]]]}
Length:      5
Docstring:  
dict() -> new empty dictionary
dict(mapping) -> new dictionary initialized from a mapping object's
    (key, value) pairs
dict(iterable) -> new dictionary initialized as if via:
    d = {}
    for k, v in iterable:
        d[k] = v
dict(**kwargs) -> new dictionary initialized with the name=value pairs
    in the keyword argument list.  For example:  dict(one=1, two=2)
# objectname is specific in each topojson file
geojson_dict = serialize_as_geojson(topojson_dict, objectname='data') 

# check if it is correct geojson
gdf = gpd.GeoDataFrame.from_features(geojson_dict["features"])
gdf.plot()
image

Btw, the dequantization on arcs happens here:

https://github.com/mattijn/topojson/blob/master/topojson/utils.py#L445

The reason why there is a dequantization of multi(points) in the geometry function is because there is only a top-level storage of arcs in the topojson specification and strangely not for the (multi)point.

from topojson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.