Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total

Export a GoogleEarth KML file to CSV output

# Parses a GoogleEarth KML file and writes the pertinent data to a CSV file

#<?xml version="1.0" encoding="UTF-8"?>
# <kml xmlns="http://earth.google.com/kml/2.0">
# <Folder>
# <name>Folder Name</name>
# <open>1</open>
# <ScreenOverlay>
# ... Screen Overlays
# </ScreenOverlay>
# <Document>
# <name>Document Name</name>
# <open>1</open>
# <Schema parent="Placemark" name="S_Parcel_centroids_SSSS">
# ... Schema Def ...
# </Schema>
# <Style id="khStyle722">
# ... Style Info ...
# </Style>
# <Folder id="layer 0">
# <name>FolderName</name>
# <Location>
# <name>Location name 1</name>
# <description><![CDATA[...Description text...]]></description>
# <styleUrl>#khStyle16037</styleUrl>
# <Point>
# <altitudeMode>relativeToGround
# <coordinates>-81.85829063169155,29.12257052899974,100</coordinates>
# </Point>
# <Column1>Column 1 data</Column1>
# <Column2>Column 2 data</Column2>
# </Location>
# <Location>
# <name>Location name 2</name>
# <description><![CDATA[...Description text...]]></description>
# <styleUrl>#khStyle16037</styleUrl>
# <Point>
# <altitudeMode>relativeToGround
# <coordinates>-81.85829063169155,29.12257052899974,100</coordinates>
# </Point>
# <Column1>Column 1 data</Column1>
# <Column2>Column 2 data</Column2>
# </Location>
#
# ... More Locations ...
# </Folder>
# </Document>
# </Folder>
# </kml>


require "rexml/document"
include REXML
kmlroot = (Document.new File.new "data.kml").root
nodes = kmlroot.elements.to_a("//Location")

begin
	f = File.open("data.csv", "w")
	f << "id,column1,column2,latitude,longitude"\n"
	id = 1
	nodes.each { |node|
		column1 = node.elements["column1"].text
		column2 = node.elements["column2"].text
		coords = node.elements["Point"].elements["coordinates"].text.split(",")
		f << [id, column1, column2, coords[1], coords[0] ].join(",") << "\n"
		id += 1
	}
ensure
	f.close
end

SQL export in zope

Takes the result of a Zope SQL query, and an ordered list of field names, and outputs a CSV file as the resultant web page. Should be called like this:

return context.sql.ordered-sql2csv(context.sql.exportOrders().dictionaries(), ["fullname","email","phone","address","town","postcode","children","adults","comments"])


## Script (Python) "ordered-sql2csv"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=sql, fo
##title=
##
container.REQUEST.RESPONSE.setHeader("Content-Type", "text/csv", 0)
sep = ""
for dk in fo:
  container.REQUEST.RESPONSE.write(sep + dk)
  sep = ","
sep = "\n"

for rec in sql:
  for i in range(0,len(fo)):
    data=rec[fo[i]]
    dl = str(data).split(',')
    data1 = " ".join(dl)
    container.REQUEST.RESPONSE.write(sep + str(data1))
    sep = ","
  sep="\n"

load a CSV file

    read-csv: func [
        "Load and parse a delimited text file."
        file [file!]
        /with
            delimiter
        /local lines
    ][
        if not with [delimiter: ","]
        lines: read/lines file
        forall lines [
            change/only lines parse/all first lines delimiter
        ]
        ; head lines  ;<- needed for View < 1.3
    ]

Read CSV and plot



oaks <- read.table("assign1/oaksdf.csv",sep=",")
oakpts <- as.points(oaks[,1],oaks[,2])
plot(oakpts)
« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total