Last active
December 20, 2015 05:09
-
-
Save michaelaye/6075850 to your computer and use it in GitHub Desktop.
Notebook showing how I generate a KML polygon with pykml and some self-coded pds label tools.
This file contains hidden or 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
| { | |
| "metadata": { | |
| "name": "" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from osgeo import gdal\n", | |
| "from lxml import etree\n", | |
| "from pymars import pdstools\n", | |
| "reload(pdstools)\n", | |
| "from pykml.factory import KML_ElementMaker as KML\n", | |
| "from pykml.factory import GX_ElementMaker as GX" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 7 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "label = pdstools.PDSLabel('/Users/maye/data/hirise/inca/ESP_022607_0985_RED.LBL')" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 2 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "stylename = \"sn_ylw-pushpin\"\n", | |
| "stylename2 = \"msn_ylw-pushpin\"\n", | |
| "stylename3 = \"sh_ylw-pushpin\"\n", | |
| "doc = KML.kml(\n", | |
| " KML.Document(\n", | |
| " KML.Name(\"Test Position\"),\n", | |
| " KML.Style(\n", | |
| " KML.IconStyle(\n", | |
| " KML.scale(1.1),\n", | |
| " KML.Icon(\n", | |
| " KML.href(\"http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png\")\n", | |
| " ),\n", | |
| " ),\n", | |
| " KML.LineStyle(\n", | |
| " KML.color('ffff0000'),\n", | |
| " KML.width('3')\n", | |
| " ),\n", | |
| " KML.PolyStyle(\n", | |
| " KML.color('800080ff'),\n", | |
| " ),\n", | |
| " id=stylename\n", | |
| " ),\n", | |
| " KML.StyleMap(\n", | |
| " KML.Pair(\n", | |
| " KML.key('normal'),\n", | |
| " KML.styleUrl('#{0}'.format(stylename)),\n", | |
| " ),\n", | |
| " KML.Pair(\n", | |
| " KML.key('highlight'),\n", | |
| " KML.styleUrl('#{0}'.format(stylename3)),\n", | |
| " ),\n", | |
| " id=stylename2\n", | |
| " ),\n", | |
| " KML.Style(\n", | |
| " KML.IconStyle(\n", | |
| " KML.scale(1.3),\n", | |
| " KML.Icon(\n", | |
| " KML.href(\"http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png\")\n", | |
| " ),\n", | |
| " ),\n", | |
| " KML.LineStyle(\n", | |
| " KML.color('ffff0000'),\n", | |
| " KML.width(3)\n", | |
| " ),\n", | |
| " KML.PolyStyle(\n", | |
| " KML.color('800080ff')\n", | |
| " ),\n", | |
| " id=stylename3\n", | |
| " ),\n", | |
| " )\n", | |
| ")\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 3 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "pm_poly= KML.Placemark(\n", | |
| " KML.name(\"Some Inca\"),\n", | |
| " KML.LookAt(\n", | |
| " KML.longitude(str(label.meanlon-360)),\n", | |
| " KML.latitude(str(label.meanlat)),\n", | |
| " KML.altitude('0'),\n", | |
| " KML.heading('0'),\n", | |
| " KML.tilt('0'),\n", | |
| " KML.range('88600'),\n", | |
| " GX.altitudeMode('relativeToSeaFloor')\n", | |
| " ),\n", | |
| " KML.styleUrl('#{0}'.format(stylename2)),\n", | |
| " KML.Polygon(\n", | |
| " KML.tessellate('1'),\n", | |
| " KML.outerBoundaryIs(\n", | |
| " KML.LinearRing(\n", | |
| " KML.coordinates(\n", | |
| " \"{maxlat},{westmost}\\n{minlat},{westmost}\\n\"\n", | |
| " \"{minlat},{eastmost}\\n{maxlat},{eastmost}\\n\"\n", | |
| " \"{maxlat},{westmost}\".format(westmost=label.maxlat,\n", | |
| " maxlat=label.westmost-360,\n", | |
| " eastmost=label.minlat,\n", | |
| " minlat=label.eastmost-360)\n", | |
| " ),\n", | |
| " ),\n", | |
| " ),\n", | |
| " ),\n", | |
| ")" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 4 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "doc.Document.append(pm_poly)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 5 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "outfile = open('testring.kml','w')\n", | |
| "outfile.write(etree.tostring(doc, pretty_print=True))\n", | |
| "outfile.close()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 8 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "print etree.tostring(doc, pretty_print=True)" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "<kml xmlns:gx=\"http://www.google.com/kml/ext/2.2\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns=\"http://www.opengis.net/kml/2.2\">\n", | |
| " <Document>\n", | |
| " <Name>Test Position</Name>\n", | |
| " <Style id=\"sn_ylw-pushpin\">\n", | |
| " <IconStyle>\n", | |
| " <scale>1.1</scale>\n", | |
| " <Icon>\n", | |
| " <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n", | |
| " </Icon>\n", | |
| " </IconStyle>\n", | |
| " <LineStyle>\n", | |
| " <color>ffff0000</color>\n", | |
| " <width>3</width>\n", | |
| " </LineStyle>\n", | |
| " <PolyStyle>\n", | |
| " <color>800080ff</color>\n", | |
| " </PolyStyle>\n", | |
| " </Style>\n", | |
| " <StyleMap id=\"msn_ylw-pushpin\">\n", | |
| " <Pair>\n", | |
| " <key>normal</key>\n", | |
| " <styleUrl>#sn_ylw-pushpin</styleUrl>\n", | |
| " </Pair>\n", | |
| " <Pair>\n", | |
| " <key>highlight</key>\n", | |
| " <styleUrl>#sh_ylw-pushpin</styleUrl>\n", | |
| " </Pair>\n", | |
| " </StyleMap>\n", | |
| " <Style id=\"sh_ylw-pushpin\">\n", | |
| " <IconStyle>\n", | |
| " <scale>1.3</scale>\n", | |
| " <Icon>\n", | |
| " <href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>\n", | |
| " </Icon>\n", | |
| " </IconStyle>\n", | |
| " <LineStyle>\n", | |
| " <color>ffff0000</color>\n", | |
| " <width>3</width>\n", | |
| " </LineStyle>\n", | |
| " <PolyStyle>\n", | |
| " <color>800080ff</color>\n", | |
| " </PolyStyle>\n", | |
| " </Style>\n", | |
| " <Placemark>\n", | |
| " <name>Some Inca</name>\n", | |
| " <LookAt>\n", | |
| " <longitude>-64.2178127911</longitude>\n", | |
| " <latitude>-81.3880681549</latitude>\n", | |
| " <altitude>0</altitude>\n", | |
| " <heading>0</heading>\n", | |
| " <tilt>0</tilt>\n", | |
| " <range>88600</range>\n", | |
| " <gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>\n", | |
| " </LookAt>\n", | |
| " <styleUrl>#msn_ylw-pushpin</styleUrl>\n", | |
| " <Polygon>\n", | |
| " <tessellate>1</tessellate>\n", | |
| " <outerBoundaryIs>\n", | |
| " <LinearRing>\n", | |
| " <coordinates>-64.6805775994,-81.2749237185\n", | |
| "-63.7550479827,-81.2749237185\n", | |
| "-63.7550479827,-81.5012125913\n", | |
| "-64.6805775994,-81.5012125913\n", | |
| "-64.6805775994,-81.2749237185</coordinates>\n", | |
| " </LinearRing>\n", | |
| " </outerBoundaryIs>\n", | |
| " </Polygon>\n", | |
| " </Placemark>\n", | |
| " </Document>\n", | |
| "</kml>\n", | |
| "\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 9 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [] | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment