How do I use the topojson.py created by calvinmetcalf: https://github.com/calvinmetcalf/topojson.py

There is a test file at git, which I tried to modify, but unfortunately I don't really know the python syntax. So I don't know how to initialize the calsses etc.

import json
import unittest

from topojson.conversion import convert

class TestTopojson(unittest.TestCase):

    def setUp(self):
        with open(r"C:\Users\root\Desktop\2016\Bundeslaender.geojson") as f:
            self.square_geojson = json.load(f)

    def test_convert_geojson_to_topojson(self):
        tj = convert(self.square_geojson)
        self.assertEqual(tj['type'], 'Topology')

if __name__=="__main__":

    test1 = TestTopojson('test1')

I'm looking for a good and simple step by step tutorial on using the topojson.py classes. I think that using python would be the best approach to easily convert geojson to topojson on a Windows machine ..

  • Welcome to gis.stackexchange! Please edit the title of your question to include enough information for future visitors to be able to find this thread when looking for the same problem. – underdark Sep 22 '16 at 18:48
  • I really don't know how to ask this question different .. It is about how to use the topojson.py (link provided) Any suggestions welcome – Leo.1906 Sep 22 '16 at 21:50

It's true that is not very clear. A simple example:

from topojson.conversion import convert
from topojson import geojson
import json

with open("test.geojson") as f:
  test_geojson = json.load(f)
print test_geojson
{u'type': u'FeatureCollection', u'features': [{u'geometry': {u'type': u'Polygon', u'coordinates': [[[-83.23145862723665, 42.61719935058299], [-83.2316352368618, 42.61737853618379], [-83.23166951279292, 42.61742295755952], [-83.23119346860754, 42.617678900083554], [-83.23097637034452, 42.6174586310034], [-83.23145862723665, 42.61719935058299]]]}, u'type': u'Feature', u'properties': {u'LOWPARCELID': u'1902226080'}}]}
# to TopoJSON
tj = convert(test_geojson)
print tj
{'objects': {'name': {u'type': 'GeometryCollection', 'geometries': [{u'type': u'Polygon', 'properties': {u'LOWPARCELID': u'1902226080'}, 'arcs': [[0]]}]}}, 'type': 'Topology', 'bbox': [-83.23166951279292, 42.61719935058299, -83.23097637034452, 42.617678900083554], 'arcs': [[[3042, 0], [-2548, 3736], [-494, 926], [6867, 5337], [3132, -4593], [-6957, -5406]]], 'transform': {'translate': [-83.23166951279292, 42.61719935058299], 'scale': [6.932117695749783e-08, 4.795974603069803e-08]}}
# to GeoJSON
print geojson(tj)
{'type': 'FeatureCollection', 'features': [{'geometry': {'type': u'Polygon', 'coordinates': [[[-83.23145863777262, 42.61719935058299], [-83.2316352681315, 42.617378528194166], [-83.23166951279292, 42.617422938918985], [-83.23119348427075, 42.617678900083554], [-83.23097637034452, 42.617458620970034], [-83.23145863777262, 42.61719935058299]]]}, 'type': 'Feature', 'properties': {u'LOWPARCELID': u'1902226080'}}]}

But it works only with GeoJSON files with a features key (FeatureCollection)

test =  { "type": "Polygon",
 "coordinates": [
   [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]
tj = convert(test)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "topojson/conversion.py", line 22, in convert
output_dict = topology(input_dict, *args, **kwargs)
File "topojson/topology.py", line 20, in topology
File "topojson/bounds.py", line 20, in bound
File "topojson/mytypes.py", line 46, in obj
File "topojson/mytypes.py", line 19, in FeatureCollection
for feature in collection['features']:
KeyError: 'features'


The result of setup.py build/install is not correct with the setup.py of the git repository

A correct structure for Python 2.7.x is

enter image description here

The init.py file is init.py

Then (with see "dir for humans")

import topojson
from see import see
.geojson()     .topojson()
# therefore
from topojson.topojson.conversion import convert
from topojson.geojson import geojson
  • Well .. I'm still having troubles using the code .. I tried your version and adjustet the test.geojson file to one of mine. But the errors start at the very beginning, because I'm not able to make the from topojson import geojson It throws errors at this point. Are you using the git repository I linked above? – Leo.1906 Sep 22 '16 at 21:49
  • look above for a solution – gene Sep 24 '16 at 15:57
  • Ok, thanks for your help. Got it to work right now .. but the library just accept very few geojson formats. I converted a shapefile to geosjon using ogr2ogr and ogr created a very clean and perfect geosjon file .. but the topojson library can't load it .. :/ – Leo.1906 Oct 1 '16 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.