#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import json
#from myModule import myModule
#===============================================================================
def convertToWKT(filename, outfile):
	newdata = []
	with open(filename) as json_file:
		data = json.load(json_file)
		for d in data["features"]:
			prop = d["properties"]
			print("POLYGON: ", len(d))
			i = 0
			if len(d["geometry"]["coordinates"][0]) > 1:
				print(i)
				wkt = "POLYGON (( "
				for p in d["geometry"]["coordinates"][0]:
					wkt+=str(p[0])+" "+ str(p[1]) +", "
				i+=1
				wkt=wkt[:-2]+" ))"
				print(wkt)
				
				newdata.append({"wkt":wkt, "properties":prop})

	with open(outfile, 'w') as outfile:
		json.dump(newdata, outfile)
#===============================================================================
def main(argv):

	if len(sys.argv) != 3:
		print ("Usage text")
	else:
		convertToWKT(argv[1], argv[2])


if __name__ == "__main__":
	main(sys.argv)
