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

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

New home to record (or in some cases landfill) scripts related to import activity.

  • Property svn:executable set to *
File size: 3.8 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 mirror City CarShare "pods" into osm
7##
8##  See also:
9##      http://wiki.openstreetmap.org/wiki/Tag:amenity%3Dcar_sharing
10##
11##  Future work:
12##      Support capacity=* tag
13##      Warn human about location discrepencies over 100 meters
14##
15import sys
16sys.path = ['../../applications/utils/import/osmsync/'] + sys.path
17from osmsync import osmsync
18
19import sys, re, urllib, urllib2
20import csv, demjson
21import xml.sax.saxutils
22
23from   pprint     import pprint
24from   xml.etree  import ElementTree
25
26class osmsync_ccs(osmsync):
27    """ Mirror merge City CarShare reservation system data into OSM """
28
29    #   Load all car sharing locations from CityCarShare
30    #   This is done via a scrape from the map.  An xml dump
31    #   previously existed, but was discontinued due to an
32    #   unrelated change of backend systems.
33    #
34    #   The maps html file has handy almost-json format records as so:
35    #    var pod_1 = {
36    #        id     :   1,
37    #        lat    :   37.783055,
38    #        lon    :   -122.405573,
39    #        name   :   "San Francisco/SOMA: 5th & Mission",
40    #        addr   :   "833 Mission St",
41    #        url    :   "http://www.citycarshare.org/pod_1.do",
42    #        vtype  :   ",9,,9,,17,",
43    #        vstring:   "Scion xB,Scion xB,Honda Fit",
44    #        gmark  :   null
45    #        }
46    #   I apologize it is my own fault that's almost-json not json.  Sorry.
47    #
48    def fetch_source(self, sourcedata):
49        sourcenodes = {}
50
51        req = urllib2.Request(sourcedata, headers=self.http_headers);
52        body = urllib2.urlopen(req).read()
53
54        pods_re = re.compile("var pod_\d* = ({.*?})", re.DOTALL)
55        for m in pods_re.finditer(body):
56            ccs_pod = demjson.decode(m.group(1))
57            pkey    = ccs_pod['id']
58            commas  = ccs_pod['vstring'].strip().count(',')
59
60            osmnode = {}
61            osmnode['tag']                  = {}
62            osmnode['lat']                  = ccs_pod['lat']
63            osmnode['lon']                  = ccs_pod['lon']
64            osmnode['tag']['amenity']       = 'car_sharing'
65            osmnode['tag']['operator']      = 'City CarShare'
66            osmnode['tag']['capacity']      = str(commas + 1)
67            osmnode['tag']['name']          = ccs_pod['name'].strip()
68            osmnode['tag']['website']       = ccs_pod['url'].strip()
69            osmnode['tag']['description']   = ccs_pod['addr'] .strip()+ " with " + ccs_pod['vstring'].strip()
70            osmnode['tag']['source']        = 'osmsync:ccs'
71            osmnode['tag']['source:pkey']   = pkey
72            sourcenodes[pkey] = osmnode
73
74        source_is_master_for=['name','website','description','amenity','operator','source','capacity']
75        return(sourcenodes, source_is_master_for)
76
77
78    #   Override a few keys
79    def record_action(self, osmnode, action):
80        if 'source:website' in osmnode['tag']:
81            del osmnode['tag']['source:website']
82        return(osmsync.record_action(self, osmnode, action))
83
84
85#############################################################################################
86#############################################################################################
87changeSetTags = {
88    'source'            : 'osmsync:ccs',
89    'source:website'    : 'http://www.citycarshare.org/',
90    'conflation_key'    : 'source:pkey',
91    'note'              : 'Prepared for human review by osmsync: '+
92                          'reads the car share reservation system and suggests matching updates for osm.'
93    }
94myfetch = osmsync_ccs()
95myfetch.run(description='City CarShare pod location mirror script',
96            ext_url='http://www.citycarshare.org/sfmap.do',
97            osm_url=osmsync.xapi_url + urllib.quote("node[source=osmsync:ccs]"),
98            )
99myfetch.output(changeSetTags)
Note: See TracBrowser for help on using the repository browser.