If you can install libraries, django has some good tools for working with geometric objects, and these objects have a geojson attribute giving you access to the GeoJSON representation of the object:
https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/
>>> from django.contrib.gis.geos import Polygon, Point, MultiPoint, GeometryCollection >>> >>> poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) ) >>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly) >>> gc.geojson u'{ "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [ 0.0, 0.0 ] }, { "type": "MultiPoint", "coordinates": [ [ 0.0, 0.0 ], [ 1.0, 1.0 ] ] }, { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 1.0 ], [ 1.0, 1.0 ], [ 0.0, 0.0 ] ] ] } ] }'
GeometryCollection can also accept a list of geometric objects:
>>> polys = [] >>> for i in range(5): ... poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) ) ... polys.append(poly) ... >>> gc = GeometryCollection(polys)
Update 2019:
shapely with shapely-geojson is now available, it can be easier to enter, since it does not require django.