'Import L0 Admin Boundaries into the Locations table from GADMv1
- designed to be called from import_admin_areas()
- assumes that basic prepop has been done, so that no new records need to be created
@param ogr - The OGR Python module
@param countries - List of ISO2 countrycodes to download data for
defaults to all countries'

@staticmethod
def import_gadm1_L0(ogr, countries=[]):

    db = current.db
    s3db = current.s3db
    ttable = s3db.gis_location_tag
    table = db.gis_location
    layer = {'url': 'http://gadm.org/data/gadm_v1_lev0_shp.zip', 'zipfile': 'gadm_v1_lev0_shp.zip', 'shapefile': 'gadm1_lev0', 'codefield': 'ISO2', 'code2field': 'ISO'}
    cwd = os.getcwd()
    TEMP = os.path.join(cwd, 'temp')
    if (not os.path.exists(TEMP)):
        import tempfile
        TEMP = tempfile.gettempdir()
    tempPath = os.path.join(TEMP, 'GADMv1')
    if (not os.path.exists(tempPath)):
        try:
            os.mkdir(tempPath)
        except OSError:
            current.log.error(('Unable    to    create    temp    folder    %s!' % tempPath))
            return
    os.chdir(tempPath)
    layerName = layer['shapefile']
    fileName = layer['zipfile']
    if (not os.path.isfile(fileName)):
        from gluon.tools import fetch
        url = layer['url']
        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.error('Open    failed.\n')
        return
    lyr = ds.GetLayerByName(layerName)
    lyr.ResetReading()
    codeField = layer['codefield']
    code2Field = layer['code2field']
    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:
                query = (((table.id == ttable.location_id) & (ttable.tag == 'ISO2')) & (ttable.value == code))
                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
                code2 = feat.GetField(code2Field)
                try:
                    id = db(query).select(table.id, limitby=(0, 1)).first().id
                    query = (table.id == id)
                    db(query).update(gis_feature_type=gis_feature_type, wkt=wkt)
                    ttable.insert(location_id=id, tag='ISO3', value=code2)
                except db._adapter.driver.OperationalError as e:
                    current.log.error(sys.exc_info[1])
        else:
            current.log.debug('No    geometry\n')
    ds.Destroy()
    db.commit()
    os.chdir(cwd)
    return
