'Produce a static GeoJSON[P] feed of Facility data
Designed to be run on a schedule to serve a high-volume website'

@staticmethod
def org_facility_geojson(jsonp=True, decimals=4):

    from shapely.geometry import Point
    from ..geojson import dumps
    db = current.db
    s3db = current.s3db
    stable = s3db.org_facility
    ltable = db.org_site_facility_type
    ttable = db.org_facility_type
    gtable = db.gis_location
    ntable = s3db.req_site_needs
    formatter = ('.%sf' % decimals)
    query = (((stable.deleted != True) & (stable.obsolete != True)) & (gtable.id == stable.location_id))
    lquery = ((ntable.deleted != True) & (ntable.site_id == stable.site_id))
    left = [ntable.on(lquery), ltable.on((stable.site_id == ltable.site_id)), ttable.on((ttable.facility_type_id == ltable.facility_type_id))]
    facs = db(query).select(stable.id, stable.name, ttable.name, stable.comments, stable.opening_times, stable.phone1, stable.phone2, stable.email, stable.website, ntable.needs, gtable.addr_street, gtable.L1, gtable.L4, gtable.lat, gtable.lon, left=left)
    features = []
    append = features.append
    for f in facs:
        g = f.gis_location
        x = g.lon
        y = g.lat
        if ((x is None) or (y is None)):
            continue
        x = float(format(x, formatter))
        y = float(format(y, formatter))
        shape = Point(x, y)
        geojson = dumps(shape, separators=SEPARATORS)
        o = f.org_facility
        properties = {'id': o.id, 'name': o.name}
        if f.get('org_facility_type.name'):
            properties['type'] = f['org_facility_type.name']
        if o.opening_times:
            properties['open'] = o.opening_times
        if o.comments:
            properties['comments'] = o.comments
        if g.addr_street:
            properties['addr'] = g.addr_street
        if g.L1:
            L1 = g.L1
            if (L1 == 'New    York'):
                properties['L1'] = 'NY'
            elif (L1 == 'New    Jersey'):
                properties['L1'] = 'NJ'
            else:
                properties['L1'] = L1
        if g.L4:
            properties['L4'] = g.L4
        if o.phone1:
            properties['ph1'] = o.phone1
        if o.phone2:
            properties['ph2'] = o.phone2
        if o.email:
            properties['email'] = o.email
        if o.website:
            properties['web'] = o.website
        n = f.req_site_needs
        if n:
            if n.needs:
                needs = json.loads(n.needs)
                if ('urgent' in needs):
                    properties['urgent'] = needs['urgent']
                if ('need' in needs):
                    properties['need'] = needs['need']
                if ('no' in needs):
                    properties['no'] = needs['no']
        f = dict(type='Feature', properties=properties, geometry=json.loads(geojson))
        append(f)
    data = dict(type='FeatureCollection', features=features)
    output = json.dumps(data, separators=SEPARATORS)
    if jsonp:
        filename = 'facility.geojsonp'
        output = ('grid(%s)' % output)
    else:
        filename = 'facility.geojson'
    path = os.path.join(current.request.folder, 'static', 'cache', filename)
    File = open(path, 'w')
    File.write(output)
    File.close()
