Last active
August 29, 2015 14:07
-
-
Save csmoore/6995539476fa04f9e10d to your computer and use it in GitHub Desktop.
MilitaryFeatureCsvToRuntimeMessage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# MilitaryFeatureCsvToRuntimeMessage.py | |
import csv | |
import os | |
import sys | |
import uuid | |
### Params: | |
### 1 - CSV File (.csv) | |
### 2 - Message File (.xml) | |
useDefaults = False | |
if (len(sys.argv) < 3) and (not useDefaults) : | |
print('Usage: [Input File:"MilitaryFeatureData"(.csv)] [Output File:"Message"(.xml)] ') | |
# useDefaults = True # For IDE testing | |
sys.exit(0) | |
if useDefaults : | |
currentPath = os.path.dirname(__file__) | |
dataPath = os.path.normpath(os.path.join(currentPath, r"Data/")) | |
inputFile = os.path.normpath(os.path.join(dataPath, r"Units.csv")) | |
outputFile = os.path.normpath(os.path.join(dataPath, r"Units.xml")) | |
else : | |
inputFile = sys.argv[1] | |
outputFile = sys.argv[2] | |
if not (os.path.exists(inputFile)) : | |
print 'Input file does not exist: ' | |
print ' [Input File:(.csv)] : ' + intputFile | |
sys.exit(0) | |
# csv reader snippet | |
with open(inputFile, "rb") as f_in : | |
reader = csv.reader(f_in, delimiter=',') | |
line = f_in.readline().strip() | |
# assumes that there is a header row with each column/attribute name | |
# We need these columns later | |
headerRowCols = line.split(',') | |
headerRowDict = {} | |
index = 0 | |
for col in headerRowCols : | |
headerRowDict[col] = index | |
index = index + 1 | |
# Shortcut: use the unique/objectid as key (assumes it exists and is 1st/0 column) | |
csvDict = {row[0]:row for row in reader} | |
messageFile = open(outputFile, 'w') | |
messageFile.write("<messages>\n") | |
messageCount = 0 | |
for key in csvDict : | |
messageFile.write("\t<message>\n") | |
currentRow = csvDict[key] | |
uniqueId = uuid.uuid4() | |
wkid = 4326 | |
messageType = 'position_report' | |
messageAction = 'update' | |
messageFile.write("\t\t<_id>%s</_id>\n" % uniqueId) | |
messageFile.write("\t\t<_type>%s</_type>\n" % messageType) | |
messageFile.write("\t\t<_wkid>%i</_wkid>\n" % wkid) | |
messageFile.write("\t\t<_action>%s</_action>\n" % messageAction) # = update | |
# TODO add check if this field exists | |
xField = headerRowDict['x'] | |
yField = headerRowDict['y'] | |
controlPointsString = currentRow[xField] + ',' + currentRow[yField] | |
messageFile.write("\t\t<_control_points>%s</_control_points>\n" % controlPointsString) | |
attributeIndex = 0 | |
for attribute in headerRowCols : | |
# TODO handle unicode | |
attrValAsString = str(currentRow[attributeIndex]) | |
messageFile.write("\t\t<" + attribute + ">" + attrValAsString + "</" + attribute + ">\n") | |
attributeIndex = attributeIndex + 1 | |
messageFile.write("\t</message>\n") | |
messageCount = messageCount + 1 | |
messageFile.write("</messages>\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment