'Import Admin Boundaries into the Locations table from GADMv2
- designed to be called from import_admin_areas()
- assumes that basic prepop has been done, so that no new L0 records need to be created
@param ogr - The OGR Python module
@param level - The OGR Python module
@param countries - List of ISO2 countrycodes to download data for
defaults to all countries
@ToDo: Complete this
- not currently possible to get all data from the 1 file easily
- no ISO2
- needs updating for gis_location_tag model
- only the lowest available levels accessible
- use GADMv1 for L0, L1, L2 & GADMv2 for specific lower?'

@staticmethod
def import_gadm2(ogr, level='L0', countries=[]):

    if (level == 'L0'):
        codeField = 'ISO2'
        code2Field = 'ISO'
    elif (level == 'L1'):
        codeField = 'ID_1'
        code2Field = 'ISO'
    elif (level == 'L2'):
        codeField = 'ID_2'
        code2Field = 'ID_1'
    else:
        current.log.error(('Level    %s    not    supported!' % level))
        return
    db = current.db
    s3db = current.s3db
    table = s3db.gis_location
    url = 'http://gadm.org/data2/gadm_v2_shp.zip'
    zipfile = 'gadm_v2_shp.zip'
    shapefile = 'gadm2'
    old_working_directory = os.getcwd()
    if os.path.exists(os.path.join(os.getcwd(), 'temp')):
        TEMP = os.path.join(os.getcwd(), 'temp')
    else:
        import tempfile
        TEMP = tempfile.gettempdir()
    tempPath = os.path.join(TEMP, 'GADMv2')
    try:
        os.mkdir(tempPath)
    except OSError:
        pass
    os.chdir(tempPath)
    layerName = shapefile
    fileName = zipfile
    if (not os.path.isfile(fileName)):
        from gluon.tools import fetch
        current.log.debug(('Downloading    %s' % url))
        try:
            file = fetch(url)
        except urllib2.URLError as exception:
            current.log.error(exception)
            return
        fp = StringIO(file)
    else:
        current.log.debug(('Using    existing    file    %s' % fileName))
        fp = open(fileName)
    current.log.debug(('Unzipping    %s' % layerName))
    import zipfile
    myfile = zipfile.ZipFile(fp)
    for ext in ('dbf', 'prj', 'sbn', 'sbx', 'shp', 'shx'):
        fileName = ('%s.%s' % (layerName, ext))
        file = myfile.read(fileName)
        f = open(fileName, 'w')
        f.write(file)
        f.close()
    myfile.close()
    current.log.debug(('Opening    %s.shp' % layerName))
    ds = ogr.Open(('%s.shp' % layerName))
    if (ds is None):
        current.log.debug('Open    failed.\n')
        return
    lyr = ds.GetLayerByName(layerName)
    lyr.ResetReading()
    for feat in lyr:
        code = feat.GetField(codeField)
        if (not code):
            continue
        if (countries and (code not in countries)):
            continue
        geom = feat.GetGeometryRef()
        if (geom is not None):
            if (geom.GetGeometryType() == ogr.wkbPoint):
                pass
            else:
                wkt = geom.ExportToWkt()
                if wkt.startswith('LINESTRING'):
                    gis_feature_type = 2
                elif wkt.startswith('POLYGON'):
                    gis_feature_type = 3
                elif wkt.startswith('MULTIPOINT'):
                    gis_feature_type = 4
                elif wkt.startswith('MULTILINESTRING'):
                    gis_feature_type = 5
                elif wkt.startswith('MULTIPOLYGON'):
                    gis_feature_type = 6
                elif wkt.startswith('GEOMETRYCOLLECTION'):
                    gis_feature_type = 7
                try:
                    db(query).update(gis_feature_type=gis_feature_type, wkt=wkt)
                except db._adapter.driver.OperationalError as exception:
                    current.log.error(exception)
        else:
            current.log.debug('No    geometry\n')
    ds.Destroy()
    db.commit()
    os.chdir(old_working_directory)
    return
