source: subversion/imports/ongoing/osmsync_noaa_nexrad.py @ 26655

Last change on this file since 26655 was 26638, checked in by bnesbitt, 8 years ago

Switch to final conflation key source=osmsync:noaa:nexrad

  • Property svn:executable set to *
File size: 4.6 KB
Line 
1#!/usr/bin/python
2##
3##  Author: Bryce Nesbitt, June 2011
4##  Licence: Public Domain, no rights reserved
5##
6##  osmsync module to import NOAA NEXRAD radar stations
7##
8##  See also:
9##      http://wiki.openstreetmap.org/wiki/Potential_Datasources
10##      http://wiki.openstreetmap.org/wiki/Man_made
11##
12##  Future work:
13##        Add to changeset 'source:website=http://www.ncdc.noaa.gov/oa/radar/nexrad.kmz'
14##
15import sys
16sys.path = ['../../applications/utils/import/osmsync/'] + sys.path
17from osmsync import osmsync
18
19import re, urllib, urllib2
20import csv, demjson
21import xml.sax.saxutils
22import zipfile
23
24from   pprint     import pprint
25from   xml.etree  import ElementTree
26from   StringIO   import StringIO
27
28class osmsync_noaa_nexrad(osmsync):
29
30    #  Sample NOAA data:
31    #  <wsr>
32    #    <name>KABR</name>
33    #    <description><![CDATA[SITE: KABR<BR>LOCATION: ABERDEEN<BR>...]]></description>
34    #    <Point>
35    #      <coordinates>-98.413,45.45600000000001,0</coordinates>
36    #    </Point>
37    #  </wsr>
38    def fetch_source(self, sourcedata):
39
40        # Load the NOAA data, decompress, and parse the XML
41        sourcenodes = {}
42        req      = urllib2.Request(sourcedata, headers=self.http_headers);
43        response = urllib2.urlopen(req)
44
45        compressed_stream = StringIO( response.read () )
46        plaintext_stream = zipfile.ZipFile(compressed_stream,'r')
47        tree    = ElementTree.parse(plaintext_stream.open('doc.kml'))
48   
49        for site in tree.iter('{http://earth.google.com/kml/2.0}wsr'):
50            pkey            = site.find("{http://earth.google.com/kml/2.0}name").text.strip()
51            description     = site.find("{http://earth.google.com/kml/2.0}description").text.strip()
52            point           = site.find("{http://earth.google.com/kml/2.0}Point")
53            lon,lat,ele     = point.find("{http://earth.google.com/kml/2.0}coordinates").text.split(',')
54            ele             = (float(ele)*12*2.54)/100     # feet->inches->centimeters->meters
55
56            description     = description.replace('<BR>',"\n")
57            description     = description.replace('<![CDATA[',"")
58            description     = description.replace(']]',"")
59
60            node                             = {}
61            node['tag']                      = {}
62            node['id']                       = pkey
63            node['lat']                      = lat
64            node['lon']                      = lon
65           #node['tag']['ele']               = ele
66            node['tag']['name']              = pkey
67            node['tag']['source:pkey']       = pkey
68            node['tag']['man_made']          = 'monitoring_station'
69            node['tag']['monitoring_station']= 'radar'
70            node['tag']['radar']             = 'NEXRAD'
71           #node['tag']['note']              = description
72            node['tag']['operator']          = 'NOAA'
73            node['tag']['website']           = "http://www.ncdc.noaa.gov/nexradinv/chooseday.jsp?id=" + pkey
74            sourcenodes[pkey] = node
75
76        source_is_master_for=['operator','website','description','note','radar','man_made','source:pkey','monitoring_station','name']
77        return(sourcenodes, source_is_master_for)
78
79    #   Map source primary keys to osm primary keys
80    def map_primary_keys(self, sourcenodes, osmnodes):
81        mapping = {}
82        if osmnodes:
83            for osmid,node in osmnodes.items():
84                pkey = node['tag'].get('name')
85                mapping[pkey]=osmid
86        return mapping
87
88    #   Override a few keys as each node is written out
89    def record_action(self, osmnode, action):
90        osmnode['tag']['source']         = 'osmsync:noaa:nexrad'
91        if 'note' in osmnode['tag']:
92            del osmnode['tag']['note']
93        if 'radar_transponder' in osmnode['tag']:
94            del osmnode['tag']['radar_transponder']
95        return(osmsync.record_action(self, osmnode, action))
96
97#######################################################################################
98changeSetTags = {
99    'source'            : 'osmsync:noaa:nexrad',
100    'source:website'    : 'http://www.ncdc.noaa.gov/oa/radar/nexrad.kmz',
101    'conflation_key'    : 'source:pkey',
102    'note'              : 'Prepared for human review by osmsync: '+
103                          'official NOAA NEXRAD weather radar stations'
104    }
105myfetch = osmsync_noaa_nexrad()
106myfetch.run(description='Mirror NOAA NEXRAD Weather Radar Stations into OpenStreetMap',
107            ext_url='http://www.ncdc.noaa.gov/oa/radar/nexrad.kmz',
108            osm_url=osmsync.xapi_url + urllib.quote('node[source=osmsync:noaa:nexrad]')
109            )
110myfetch.output(changeSetTags)
Note: See TracBrowser for help on using the repository browser.