# # File: sf/htdocs/books/swift/app/form.py # # This file is designed to convert tree data from a # csv format (at opentrees.org) to a geojson format. # This version generates a random subset of all geojson data objects from # opentrees.data.geojson # # This effort is part of the earthtree ios swift app which is # designed to display and map trees in the entire world! one by one! # # This geojson data will need to be provided to the app via an "api" # because the complete data is too big (currently 230 meg approximately). # I will use the codable swift protocol to convert the geojson data # to swift objects, and the UITableViews, MapKits etc to display it and # hopefully, to edit it, add to it. # # History: # june 2019 # wrote this python formatter, which is the first python # thingy I have ever written. # 27 June 2019 # various geojson schema bugs import csv import random with open('opentrees.data.csv', mode='r') as f: reader = csv.DictReader(f) s = """ { "type": "FeatureCollection", "features": [ """ print s # Output pretty printed geojson data. for row in reader: if 2 == random.randint(1,100): s = """ {{ "type": "Feature", "geometry": {{ "type": "Point", "coordinates": [{lat}, {lon}] }}, "properties": {{ "genus": "{genus}", "species": "{species}", "variety": "{variety}", "description": "{description}", "common": "{common}", "height": "{height}", "crown": "{crown}", "dbh": "{dbh}", "health": "{health}", "structure": "{structure}", "source": "{source}" }} }}, """.format(**row) print s print " ] } " # Field names in original csv file by steve bennett, at opentrees.org # Some of these fields, I wont use, eg scientific .... ## id,geom,tree_type,genus,species,scientific,variety,description,common,height,crown,dbh,health,structure,captured,planted,ule_min,ule_max,ref,location,maturity,lat,lon,source,s