Пример #1
0
    def update_regions(self):
        country_regions = self.get_json_data(
            "/../data_backup/country_regions.json")
        for cr in country_regions:
            the_country = None

            country_iso2 = cr['iso2'].lower()
            region_dac_code = cr['dac_region_code'].lower()
            region_dac_name = cr['dac_region_name'].lower()

            # Get country by iso2
            if Country.objects.filter(iso2=country_iso2).exists():
                the_country = Country.objects.get(iso2=country_iso2,
                                                  primary_name=True)

            # Update or create region
            if Region.objects.filter(code=region_dac_code).exists():
                the_region = Region.objects.get(code=region_dac_code)
                the_region.name = region_dac_name
                the_region.save()
            else:
                the_region = Region(code=region_dac_code, name=region_dac_name)
                the_region.save()

            # Create geolocation record by region
            if not Geolocation.objects.filter(tag=region_dac_name).exists():
                Geolocation(tag=region_dac_name,
                            content_object=the_region,
                            type='region').save()

            # Update the region of the country
            if the_country:
                if the_country.region is None:
                    the_country.region = the_region
                    the_country.save()
Пример #2
0
    def update_un_regions(self):
        from geodata.models import Country, Region
        import ujson
        import os
        import os.path
        from iati.models import RegionVocabulary

        base = os.path.dirname(os.path.abspath(__file__))
        location = base + "/data_backup/un_region_codes.json"

        json_data = open(location)
        un_region_codes = ujson.load(json_data)

        times = 0
        while times < 2:
            times += 1
            for cr in un_region_codes["territorycontainment"]["group"]:
                try:
                    type = cr['type']
                    contains = cr['contains']
                    region_un_name = cr['name']

                    the_region = None

                    if Region.objects.filter(code=type).exists():
                        the_region = Region.objects.get(code=type)
                        the_region.name = region_un_name
                    else:
                        the_region_voc = RegionVocabulary.objects.get(code=2)
                        the_region = Region(code=type,
                                            name=region_un_name,
                                            region_vocabulary=the_region_voc,
                                            parental_region=None)

                    the_region.save()

                    countries_or_regions = contains.split(" ")
                    try:
                        int(countries_or_regions[0])

                        for cur_region in countries_or_regions:
                            if Region.objects.filter(code=cur_region).exists():
                                cur_region_obj = Region.objects.get(
                                    code=cur_region)
                                cur_region_obj.parental_region = the_region
                                cur_region_obj.save()

                    except:
                        for cur_country in countries_or_regions:
                            if Country.objects.filter(
                                    code=cur_country).exists():
                                the_country = Country.objects.get(
                                    code=cur_country)
                                the_country.un_region = the_region
                                the_country.save()

                except Exception as e:
                    print "error in update_country_regions" + str(type)
                    print e.args
        json_data.close()
Пример #3
0
    def update_un_regions(self):
        from geodata.models import Country, Region
        import ujson
        import os
        import os.path
        from iati.models import RegionVocabulary

        base = os.path.dirname(os.path.abspath(__file__))
        location = base + "/data_backup/un_region_codes.json"

        json_data = open(location)
        un_region_codes = ujson.load(json_data)

        times = 0
        while times < 2:
            times += 1
            for cr in un_region_codes["territorycontainment"]["group"]:
                try:
                    type = cr['type']
                    contains = cr['contains']
                    region_un_name = cr['name']

                    the_region = None

                    if Region.objects.filter(code=type).exists():
                        the_region = Region.objects.get(code=type)
                        the_region.name = region_un_name
                    else:
                        the_region_voc = RegionVocabulary.objects.get(code=2)
                        the_region = Region(code=type, name=region_un_name, region_vocabulary=the_region_voc, parental_region=None)

                    the_region.save()

                    countries_or_regions = contains.split(" ")
                    try:
                        int(countries_or_regions[0])

                        for cur_region in countries_or_regions:
                            if Region.objects.filter(code=cur_region).exists():
                                cur_region_obj = Region.objects.get(code=cur_region)
                                cur_region_obj.parental_region = the_region
                                cur_region_obj.save()

                    except:
                        for cur_country in countries_or_regions:
                            if Country.objects.filter(code=cur_country).exists():
                                the_country = Country.objects.get(code=cur_country)
                                the_country.un_region = the_region
                                the_country.save()

                except Exception as e:
                    print("error in update_country_regions" + str(type))
                    print(e.args)
        json_data.close()
Пример #4
0
    def add_code_list_item(self, elem):

        tag = elem.tag
        item = None
        code = self.return_first(elem.xpath('code/text()'))
        name = self.return_first(elem.xpath('name/text()'))
        description = self.return_first(elem.xpath('description/text()')) or ''
        language_name = self.return_first(elem.xpath('language/text()'))
        category = self.return_first(elem.xpath('category/text()'))
        url = self.return_first(elem.xpath('url/text()')) or ' '
        model_name = tag

        if tag == "Country":
            name = name.lower().capitalize()
            item = Country(language=language_name, data_source="IATI")

        elif tag == "Region":
            region_voc = RegionVocabulary.objects.get(code=1)
            item = Region(region_vocabulary=region_voc)

        if name is None or name == '':
            logger.log(0, 'name is null in ' + tag)
            name = code

        model = None
        try:
            # to do; change app_label to iati_codelist after codelist app change
            model = apps.get_model(app_label='geodata', model_name=model_name)
        except LookupError:
            pass

        if not model:
            try:
                model = apps.get_model(app_label='geodata',
                                       model_name=model_name)
            except LookupError:
                print(''.join(['Model not found: ', model_name]))
                return False

        if not item:
            item = model()

        item.code = code
        item.name = name
        item.codelist_iati_version = self.looping_through_version

        item = self.add_to_model_if_field_exists(model, item, 'description',
                                                 description)
        item = self.add_to_model_if_field_exists(model, item, 'url', url)
        if category:
            item = self.add_to_model_if_field_exists(model, item,
                                                     'category_id', category)

        if item is not None and not model.objects.filter(
                pk=item.code).exists():
            try:
                item.save()
            except IntegrityError as err:
                print("Error: {}".format(err))
                pass
Пример #5
0
    def update_region_center(self):
        # region_poly = self.get_json_data("/../data_backup/continents.json")
        #
        # for region in  region_poly.get('features'):
        #     name = region.get('properties').get('CONTINENT').lower()
        #     polygon = json.dumps(region.get('geometry'))
        #     instance, created = Region.objects.get_or_create(name=name, polygons=polygon)
        #     if created:
        #         instance.save()
        #         Geolocation(content_object=instance, tag=name, type='region').save()

        region_centers = self.get_json_data(
            "/../data_backup/region_center_locations.json")

        # for r in region_centers:
        #     if Region.objects.filter(code=r).exists():
        #         current_region = Region.objects.get(code=r)
        #         point_loc_str = 'POINT(%s %s)' % (str(region_centers[r]["longitude"]), str(region_centers[r]["latitude"]))
        #         longlat = fromstr(point_loc_str, srid=4326)
        #         current_region.center_longlat = longlat
        #         current_region.save()

        for r in region_centers:
            region = Region()
            region.code = r
            point_loc_str = 'POINT(%s %s)' % (
                str(region_centers[r]["longitude"]),
                str(region_centers[r]["latitude"]))
            longlat = fromstr(point_loc_str, srid=4326)
            region.center_longlat = longlat
            region.save()
Пример #6
0
    def add_code_list_item(self, elem):
        '''Adds ALL available codelist items from the file IF element's name
        matches model name in our database (RegionVocabulary, Country, etc.)
        '''

        tag = elem.tag
        item = None
        code = smart_text(self.return_first(elem.xpath('code/text()')))
        name = smart_text(self.return_first(elem.xpath('name/text()')))
        description = smart_text(
            self.return_first(elem.xpath('description/text()'))) or ''
        language_name = smart_text(
            self.return_first(elem.xpath('language/text()')))
        category = smart_text(self.return_first(elem.xpath('category/text()')))
        url = smart_text(self.return_first(elem.xpath('url/text()'))) or ' '
        model_name = tag

        # If Codelist name in IATI's file is different from our model name (or
        # if other modifications are needed), such conditions should be
        # overriden here:

        if tag == "Country":
            name = name.lower().title()
            item = Country(language=language_name, data_source="IATI")

        elif tag == "DocumentCategory-category":
            model_name = 'DocumentCategoryCategory'

        elif tag == "FileFormat":
            FileFormat(category=category)
            category = None

        elif tag == "OrganisationRegistrationAgency":
            OrganisationRegistrationAgency(category=category)

        elif tag == "LocationType-category":
            model_name = 'LocationTypeCategory'

        elif tag == "OrganisationIdentifier":
            item = OrganisationIdentifier(abbreviation=None)

        elif tag == "IATIOrganisationIdentifier":
            model_name = 'OrganisationIdentifier'
            item = OrganisationIdentifier(abbreviation=None)

        elif tag == "SectorCategory":
            name = name.lower().capitalize()

        elif tag == "BudgetIdentifierSector-category":
            model_name = 'BudgetIdentifierSectorCategory'

        elif tag == "FinanceType-category":
            model_name = 'FinanceTypeCategory'

        elif tag == "FinanceType":
            model_name = 'FinanceType'

        elif tag == "Region":
            region_voc = RegionVocabulary.objects.get(code=1)
            item = Region(region_vocabulary=region_voc)

        elif tag == "Sector":
            sector_vocabulary = SectorVocabulary.objects.get(code=1)
            item = Sector(vocabulary=sector_vocabulary)

        elif tag == "AidType-category":
            model_name = 'AidTypeCategory'

        elif tag == "AidType":
            # Currently, only officially supported vocabularies (accessable via
            # AITI API) are supported:
            item = AidType(vocabulary=AidTypeVocabulary.objects.get(code=1))

        elif tag == "CRSAddOtherFlags":
            model_name = 'OtherFlags'

        elif tag == "CRSChannelCode":
            name = name[:255]

        elif tag == "Version":
            if url is None:
                url = 'http://reference.iatistandard.org/' + \
                    self.looping_through_version.replace('.', '')

        if name is None or name == '':
            logger.log(0, 'name is null in ' + tag)
            name = code

        model = None
        try:
            # to do; change app_label to iati_codelist after codelist app
            # change
            model = apps.get_model(app_label='iati_codelists',
                                   model_name=model_name)
        except LookupError:
            pass

        try:
            # to do; change app_label to iati_codelist after codelist app
            # change
            model = apps.get_model(app_label='iati_vocabulary',
                                   model_name=model_name)
        except LookupError:
            pass

        if not model:
            try:
                model = apps.get_model(app_label='geodata',
                                       model_name=model_name)
            except LookupError:
                print(''.join(['Model not found: ', model_name]))
                return False

        if not item:
            item = model()

        item.code = code
        item.name = name

        if len(item.name) > 200:
            item.name = item.name[0:200]
            print("name of code: {} , name: {} shortened to 200".format(
                item.code, item.name))

        item.codelist_iati_version = self.looping_through_version

        item = self.add_to_model_if_field_exists(model, item, 'description',
                                                 description)
        item = self.add_to_model_if_field_exists(model, item, 'url', url)

        if category and model_name == 'FinanceType':
            ft_cat = FinanceTypeCategory.objects.filter(
                # currently 'code' field is used as a PK for this model:
                code=category, ).first()

            if ft_cat:
                item.category = ft_cat

        # FIXME: refactor this general abstract approach with models!:
        if category:
            item = self.add_to_model_if_field_exists(model, item,
                                                     'category_id', category)

        if item is not None and not model.objects.filter(
                pk=item.code).exists():
            try:
                item.save()
            except IntegrityError as err:
                print("Error: {}".format(err))
                pass
Пример #7
0
    def add_code_list_item(self, elem):

        tag = elem.tag
        item = None
        code = self.return_first(elem.xpath('code/text()'))
        name = self.return_first(elem.xpath('name/text()'))
        description = self.return_first(elem.xpath('description/text()')) or ''
        language_name = self.return_first(elem.xpath('language/text()'))
        category = self.return_first(elem.xpath('category/text()'))
        url = self.return_first(elem.xpath('url/text()')) or ' '
        model_name = tag

        if tag == "Country":
            name = name.lower().title()
            item = Country(language=language_name, data_source="IATI")

        elif tag == "DocumentCategory-category":
            model_name = 'DocumentCategoryCategory'

        elif tag == "FileFormat":
            FileFormat(category=category)
            category = None

        elif tag == "OrganisationRegistrationAgency":
            OrganisationRegistrationAgency(category=category)

        elif tag == "LocationType-category":
            model_name = 'LocationTypeCategory'

        elif tag == "OrganisationIdentifier":
            item = OrganisationIdentifier(abbreviation=None)

        elif tag == "IATIOrganisationIdentifier":
            model_name = 'OrganisationIdentifier'
            item = OrganisationIdentifier(abbreviation=None)

        elif tag == "SectorCategory":
            name = name.lower().capitalize()

        elif tag == "BudgetIdentifierSector-category":
            model_name = 'BudgetIdentifierSectorCategory'

        elif tag == "FinanceType-category":
            model_name = 'FinanceTypeCategory'

        elif tag == "Region":
            region_voc = RegionVocabulary.objects.get(code=1)
            item = Region(region_vocabulary=region_voc)

        elif tag == "Sector":
            sector_vocabulary = SectorVocabulary.objects.get(code=1)
            item = Sector(vocabulary=sector_vocabulary)

        elif tag == "AidType-category":
            model_name = 'AidTypeCategory'

        elif tag == "CRSAddOtherFlags":
            model_name = 'OtherFlags'

        elif tag == "CRSChannelCode":
            name = name[:255]

        elif tag == "Version":
            if url is None:
                url = 'http://iatistandard.org/' + self.looping_through_version.replace(
                    '.', '')

        if name is None or name == '':
            logger.log(0, 'name is null in ' + tag)
            name = code

        model = None
        try:
            # to do; change app_label to iati_codelist after codelist app change
            model = apps.get_model(app_label='iati_codelists',
                                   model_name=model_name)
        except LookupError:
            pass

        try:
            # to do; change app_label to iati_codelist after codelist app change
            model = apps.get_model(app_label='iati_vocabulary',
                                   model_name=model_name)
        except LookupError:
            pass

        if not model:
            try:
                model = apps.get_model(app_label='geodata',
                                       model_name=model_name)
            except LookupError:
                print ''.join(['Model not found: ', model_name])
                return False

        if not item:
            item = model()

        item.code = code
        item.name = name
        item.codelist_iati_version = self.looping_through_version

        item = self.add_to_model_if_field_exists(model, item, 'description',
                                                 description)
        item = self.add_to_model_if_field_exists(model, item, 'url', url)
        if category:
            item = self.add_to_model_if_field_exists(model, item,
                                                     'category_id', category)

        if item is not None and not model.objects.filter(
                pk=item.code).exists():
            try:
                item.save()
            except IntegrityError as err:
                print("Error: {}".format(err))
                pass
Пример #8
0
        def add_code_list_item(elem):
            tag = elem.tag

            db_row = None

            code = elem.xpath('code/text()')
            name = elem.xpath('name/text()')
            description = elem.xpath('description/text()')
            language_name = elem.xpath('language/text()')
            category = elem.xpath('category/text()')
            category_name = elem.xpath('category-name/text()')
            category_description = elem.xpath('category-description/text()')
            abbreviation = elem.xpath('abbreviation/text()')
            sector = elem.xpath('sector/text()')
            url = elem.xpath('url/text()')

            if not code:
                code = ""
            else:
                code = return_first_exist(code)
            if not name:
                name = ""
            else:
                name = return_first_exist(name)
            if not description:
                description = ""
            else:
                description = return_first_exist(description)
            if not language_name:
                language_name = ""
            else:
                language_name = return_first_exist(language_name)
            if not category:
                category = ""
            else:
                category = return_first_exist(category)
            if not category_name:
                category_name = ""
            else:
                category_name = return_first_exist(category_name)
            if not category_description:
                category_description = ""
            else:
                category_description = return_first_exist(category_description)
            if not abbreviation:
                abbreviation = ""
            else:
                abbreviation = return_first_exist(abbreviation)
            if not sector:
                sector = ""
            else:
                sector = return_first_exist(sector)

            try:
                if tag == "ActivityDateType":
                    db_row = ActivityDateType(code=code, name=name)

                elif tag == "ActivityStatus":
                    db_row = ActivityStatus(code=code,
                                            name=name,
                                            language=language_name)

                elif tag == "Country":
                    name = name.lower().capitalize()
                    db_row = Country(code=code,
                                     name=name,
                                     language=language_name,
                                     data_source="IATI")

                elif tag == "BudgetType":
                    db_row = BudgetType(code=code,
                                        name=name,
                                        language=language_name)

                elif tag == "CollaborationType":
                    db_row = CollaborationType(code=code,
                                               name=name,
                                               description=description,
                                               language=language_name)

                elif tag == "ConditionType":
                    db_row = ConditionType(code=code,
                                           name=name,
                                           language=language_name)

                elif tag == "Currency":
                    db_row = Currency(code=code,
                                      name=name,
                                      language=language_name)

                elif tag == "DescriptionType":
                    db_row = DescriptionType(code=code,
                                             name=name,
                                             description=description)

                elif tag == "DisbursementChannel":
                    db_row = DisbursementChannel(code=code, name=name)

                elif tag == "DocumentCategory-category":
                    db_row = DocumentCategoryCategory(code=code, name=name)

                elif tag == "DocumentCategory":
                    dcc = DocumentCategoryCategory.objects.get(code=category)
                    db_row = DocumentCategory(code=code,
                                              name=name,
                                              description=description,
                                              category=dcc)

                elif tag == "GeographicLocationClass":
                    db_row = GeographicLocationClass(code=code, name=name)

                elif tag == "FileFormat":
                    db_row = FileFormat(code=code, name=name)

                elif tag == "FlowType":
                    db_row = FlowType(code=code,
                                      name=name,
                                      description=description)

                elif tag == "GazetteerAgency":
                    db_row = GazetteerAgency(code=code, name=name)

                elif tag == "GeographicalPrecision":
                    db_row = GeographicalPrecision(code=code,
                                                   name=name,
                                                   description=description)

                elif tag == "IndicatorMeasure":
                    db_row = ResultIndicatorMeasure(code=code, name=name)

                elif tag == "Language":
                    db_row = Language(code=code, name=name)

                elif tag == "LocationType-category":
                    db_row = LocationTypeCategory(code=code, name=name)

                elif tag == "LocationType":
                    ltc = LocationTypeCategory.objects.get(code=category)
                    db_row = LocationType(code=code,
                                          name=name,
                                          description=description,
                                          category=ltc)

                elif tag == "OrganisationIdentifier":
                    db_row = OrganisationIdentifier(code=code,
                                                    abbreviation=abbreviation,
                                                    name=name)

                elif tag == "OrganisationRole":
                    db_row = OrganisationRole(code=code,
                                              name=name,
                                              description=description)

                elif tag == "OrganisationType":
                    db_row = OrganisationType(code=code, name=name)

                elif tag == "PolicyMarker":
                    db_row = PolicyMarker(code=code, name=name)

                elif tag == "PolicySignificance":
                    db_row = PolicySignificance(code=code,
                                                name=name,
                                                description=description)

                elif tag == "PublisherType":
                    db_row = PublisherType(code=code, name=name)

                elif tag == "RelatedActivityType":
                    db_row = RelatedActivityType(code=code,
                                                 name=name,
                                                 description=description)

                elif tag == "ResultType":
                    db_row = ResultType(code=code, name=name)

                elif tag == "SectorCategory":
                    name = name.lower().capitalize()
                    db_row = SectorCategory(code=code,
                                            name=name,
                                            description=description)

                elif tag == "TiedStatus":
                    db_row = TiedStatus(code=code,
                                        name=name,
                                        description=description)

                elif tag == "TransactionType":
                    db_row = TransactionType(code=code,
                                             name=name,
                                             description=description)

                elif tag == "ValueType":
                    db_row = ValueType(code=code,
                                       name=name,
                                       description=description)

                elif tag == "VerificationStatus":
                    db_row = VerificationStatus(code=code, name=name)

                elif tag == "Vocabulary":
                    db_row = Vocabulary(code=code, name=name)

                elif tag == "ActivityScope":
                    db_row = ActivityScope(code=code, name=name)

                elif tag == "AidTypeFlag":
                    db_row = AidTypeFlag(code=code, name=name)

                elif tag == "BudgetIdentifier":
                    db_row = BudgetIdentifier(code=code,
                                              name=name,
                                              category=category,
                                              sector=sector)

                elif tag == "BudgetIdentifierSector-category":
                    db_row = BudgetIdentifierSectorCategory(code=code,
                                                            name=name)

                elif tag == "BudgetIdentifierSector":
                    bisc = BudgetIdentifierSectorCategory.objects.get(
                        code=category)
                    db_row = BudgetIdentifierSector(code=code,
                                                    name=name,
                                                    category=bisc)

                elif tag == "BudgetIdentifierVocabulary":
                    db_row = BudgetIdentifierVocabulary(code=code, name=name)

                elif tag == "ContactType":
                    db_row = ContactType(code=code, name=name)

                elif tag == "LoanRepaymentPeriod":
                    db_row = LoanRepaymentPeriod(code=code, name=name)

                elif tag == "LoanRepaymentType":
                    db_row = LoanRepaymentType(code=code, name=name)

                elif tag == "RegionVocabulary":
                    db_row = RegionVocabulary(code=code, name=name)

                elif tag == "FinanceType":
                    ftc = FinanceTypeCategory.objects.get(code=category)
                    db_row = FinanceType(code=code, name=name, category=ftc)

                elif tag == "FinanceType-category":
                    db_row = FinanceTypeCategory(code=code,
                                                 name=name,
                                                 description=description)

                elif tag == "Region":
                    region_voc = RegionVocabulary.objects.get(code=1)
                    db_row = Region(code=code,
                                    name=name,
                                    region_vocabulary=region_voc)

                elif tag == "AidType-category":
                    db_row = AidTypeCategory(code=code,
                                             name=name,
                                             description=description)

                elif tag == "AidType":
                    atc = AidTypeCategory.objects.get(code=category)
                    db_row = AidType(code=code,
                                     name=name,
                                     description=description,
                                     category=atc)

                elif tag == "Sector":
                    sector_cat = SectorCategory.objects.get(code=category)
                    db_row = Sector(code=code,
                                    name=name,
                                    description=description,
                                    category=sector_cat)

                # v1.04 added codelists

                elif tag == "GeographicLocationReach":
                    db_row = GeographicLocationReach(code=code, name=name)

                elif tag == "OrganisationRegistrationAgency":
                    db_row = OrganisationRegistrationAgency(
                        code=code,
                        name=name,
                        description=description,
                        category=category,
                        category_name=category_name,
                        url=url)

                elif tag == "GeographicExactness":
                    db_row = GeographicExactness(code=code,
                                                 name=name,
                                                 description=description,
                                                 category=category,
                                                 url=url)

                elif tag == "GeographicVocabulary":
                    db_row = GeographicVocabulary(code=code,
                                                  name=name,
                                                  description=description,
                                                  category=category,
                                                  url=url)

                else:
                    print "type not saved: " + tag

                if (db_row is not None):
                    db_row.save()

            except Exception as e:
                logger.info("error in codelists")
                logger.info('%s (%s)' % (e.message, type(e)))
Пример #9
0
    def update_unesco_regions(self):
        '''
        This code will loop over the XML unesco file ("Country_list_unesco.xml")

        It will try to find a unesco region and relate it to a country

        '''

        from geodata.models import Region
        import ujson
        import os
        from iati.models import RegionVocabulary

        #find the base path of the files
        base = os.path.dirname(os.path.abspath(__file__))

        #location of the unesco regions
        location = base + "/data_backup/unesco_regions.json"
        json_data = open(location)
        unesco_regions = ujson.load(json_data)

        #parsing the country XML file
        xmlDoc = etree.parse(base + "/data_backup/Country_list_unesco.xml")

        #a tracker to see how many countries the XML file contains
        counter_countries = 0

        #a tracker to see how many countries are found with a detected region
        counter_region_found_for_country = 0



        #loop over the countries in the XML file
        for indicator in xmlDoc.iter('Country'):
            #count total countries
            counter_countries += 1

            #get iso country from XML
            iso2_xml =  indicator.get('countryid').rstrip()
            #get region text
            region_text_xml = indicator[18].getchildren()[0].text


            #keeping track of unfind regions
            counter_unfind_region = 0

            #loop over the unesco regions json file #TODO create a function in a dictionary to increase performance
            for cr in unesco_regions:
                try:
                    #getting the unesco region properties from the json file
                    code = cr
                    latitude = unesco_regions[cr]['latitude']
                    longitude = unesco_regions[cr]['longitude']
                    name = unesco_regions[cr]['name']
                    if counter_unfind_region == 3:
                        print region_text_xml
                        counter_unfind_region = 0
                    #try to match the region of the XML file with the json Unesco region file
                    if name.lower() == region_text_xml.lower():
                        counter_unfind_region = 0
                        counter_region_found_for_country += 1
                        #trying to get the region form the database
                        if Region.objects.filter(code=code).exists():
                            the_region = Region.objects.get(code=code)
                            the_region.name = name

                        #create a region
                        else:
                            #region has not been found, we need to create a region vocabulary for Unesco and create a region unless it exists
                            if RegionVocabulary.objects.filter(name="UNESCO").exists():
                                the_region_voc = RegionVocabulary.objects.get(name="UNESCO")
                            else:
                                the_region_voc = RegionVocabulary(code=999, name="UNESCO")
                                the_region_voc.save()
                            point_loc_str = 'POINT(' + longitude + ' ' + latitude + ')'
                            center_location = fromstr(point_loc_str, srid=4326)
                            the_region = Region(code=code, name=name, region_vocabulary=the_region_voc, parental_region=None, center_longlat=center_location)
                            the_region.save()


                        #now we have the correct region and are we able to relate this region to the specific country
                        #find country
                        country = Country.objects.get(code=iso2_xml)
                        #add the unesco region
                        country.unesco_region = the_region
                        country.save()
                    else:
                        counter_unfind_region += 1

                except Exception as e:
                    print "error in update_country_regions" + str(type)
                    print e.args


        print "Total countries found: %s, Total regions detected: %s" % (counter_countries, counter_region_found_for_country)
        json_data.close()
Пример #10
0
    def update_unesco_regions(self):
        '''
        This code will loop over the XML unesco file ("Country_list_unesco.xml")

        It will try to find a unesco region and relate it to a country

        '''

        from geodata.models import Region
        import ujson
        import os
        from iati.models import RegionVocabulary

        #find the base path of the files
        base = os.path.dirname(os.path.abspath(__file__))

        #location of the unesco regions
        location = base + "/data_backup/unesco_regions.json"
        json_data = open(location)
        unesco_regions = ujson.load(json_data)

        #parsing the country XML file
        xmlDoc = etree.parse(base + "/data_backup/Country_list_unesco.xml")

        #a tracker to see how many countries the XML file contains
        counter_countries = 0

        #a tracker to see how many countries are found with a detected region
        counter_region_found_for_country = 0

        #loop over the countries in the XML file
        for indicator in xmlDoc.iter('Country'):
            #count total countries
            counter_countries += 1

            #get iso country from XML
            iso2_xml = indicator.get('countryid').rstrip()
            #get region text
            region_text_xml = indicator[18].getchildren()[0].text

            #keeping track of unfind regions
            counter_unfind_region = 0

            #loop over the unesco regions json file #TODO create a function in a dictionary to increase performance
            for cr in unesco_regions:
                try:
                    #getting the unesco region properties from the json file
                    code = cr
                    latitude = unesco_regions[cr]['latitude']
                    longitude = unesco_regions[cr]['longitude']
                    name = unesco_regions[cr]['name']
                    if counter_unfind_region == 3:
                        counter_unfind_region = 0
                    #try to match the region of the XML file with the json Unesco region file
                    if name.lower() == region_text_xml.lower():
                        counter_unfind_region = 0
                        counter_region_found_for_country += 1
                        #trying to get the region form the database
                        if Region.objects.filter(code=code).exists():
                            the_region = Region.objects.get(code=code)
                            the_region.name = name

                        #create a region
                        else:
                            #region has not been found, we need to create a region vocabulary for Unesco and create a region unless it exists
                            if RegionVocabulary.objects.filter(
                                    name="UNESCO").exists():
                                the_region_voc = RegionVocabulary.objects.get(
                                    name="UNESCO")
                            else:
                                the_region_voc = RegionVocabulary(
                                    code=999, name="UNESCO")
                                the_region_voc.save()
                            point_loc_str = 'POINT(' + longitude + ' ' + latitude + ')'
                            center_location = fromstr(point_loc_str, srid=4326)
                            the_region = Region(
                                code=code,
                                name=name,
                                region_vocabulary=the_region_voc,
                                parental_region=None,
                                center_longlat=center_location)
                            the_region.save()

                        #now we have the correct region and are we able to relate this region to the specific country
                        #find country
                        country = Country.objects.get(code=iso2_xml)
                        #add the unesco region
                        country.unesco_region = the_region
                        country.save()
                    else:
                        counter_unfind_region += 1

                except Exception as e:
                    print "error in update_country_regions" + str(type)
                    print e.args

        print "Total countries found: %s, Total regions detected: %s" % (
            counter_countries, counter_region_found_for_country)
        json_data.close()