Does anyone know of a library or tool ( preferrably Python)to convert true type font glyphs into polygonal shapes (for testing GIS algorithms)?
[GIS] How to generate WKT geometries from true type font glyphs (python)
fontpythonttfwell-known-text
Related Solutions
Shapely PyQGIS, and GeoDjango use the same API based on the GEOS library:
With Shapely:
with a list of points:
from shapely.geometry import Point, LineString, mapping
pt1 = Point(0,0)
pt2 = Point(20,20)
pt3 = Point (50,50)
line = LineString([pt1,pt2,pt3])
#GeoJSON format
print mapping(line)
{'type': 'LineString', 'coordinates': ((0.0, 0.0), (20.0, 20.0), (50.0, 50.0))}
or with a list of coordinates:
line = LineString([(0, 0), (20,20),(50,50)])
#GeoJSON format
print mapping(line)
{'type': 'LineString', 'coordinates': ((0.0, 0.0), (20.0, 20.0), (50.0, 50.0))}
with PyQGIS:
with a list of points:
line = QgsGeometry.fromPolyline([QgsPoint(0,0),QgsPoint(20,20),QgsPoint(50,50)])
#GeoJSON format
print line.exportToGeoJSON()
{ "type": "LineString", "coordinates": [ [0, 0], [20, 20], [50, 50] ] }
with GeoDjango (look at Django: GEOS API)
with a list of coordinates:
from django.contrib.gis.geos import LineString
line = LineString((0, 0), (20, 20), (50, 50))
# GeoJSON format
print line.json
{ "type": "LineString", "coordinates": [ [ 0.0, 0.0 ], [ 20.0, 20.0 ], [ 50.0, 50.0 ] ] }
The resulting line is made up of two segments: (0,0) to (20,20) and (20,20) to (50,50)
Since you've already got a list of dicts, you can use Shapely (to manage the geometry) and Fiona (to write the shape file).
from shapely.geometry import mapping
from shapely.wkt import loads
from fiona import collection
schema = {'geometry': 'Point', 'properties': {'atribute1':'value', 'atribute2':'value'}}
with collection("output.shp", "w", "ESRI Shapefile", schema) as output:
for point in points:
geometry = loads(point['wkt'])
output.write({'properties':{'atribute1': point['atribute1'],
'atribute2': point['atribute2']},
'geometry': mapping(geometry)
Largely cribbed from GIS with Python, Shapely, and Fiona by Tom MacWright, but I found the Shapely WKT info in the Shapely docs
Edit in response to @Mathew edit in original question: There are three different errors that I find I'm getting, and your version of Fiona may be calling them slightly different.
- If
'datetime.datetime'
is used instead of'datetime'
I get a'ValueError'
as'datetime.datetime'
is not in the list of valid property types. - If I'm trying to write somewhere outside my working directory, I'll get
ERROR:Fiona:OGR Error 1: Failed to create file .shp file.
Try without specifying a directory in the file path. - I'm getting a
Warning:Fiona:OGR Error 6: Field time create as date field, though DateTime requested.
Which looks to be because shapefiles don't specifically allow a datetime field. You can separate out the datetimes into 2 separate properties, or write them as a string.
Best Answer
1) You can use TTFQuery as in Retrieving bounding box and bezier curve data for all glyphs in a .ttf font file in python
Now use glyph calculateContours()
Convert to WKT format with Shapely for example
2) you can also use freetype-py