Ejemplo n.º 1
0
    def run(self):
        """
        Generate the hierarchy.
        """
        with self.user.progress(_("Generating hierarchy"), '',
                                self.db.get_number_of_places()) as step:

            for handle in self.db.get_place_handles():
                step()
                place = self.db.get_place_from_handle(handle)

                if (int(place.get_type()) != PlaceType.UNKNOWN
                        or ',' not in place.get_title()):
                    # Only process places with a type of Unknown.
                    continue

                names = [
                    name.strip() for name in place.get_title().split(',')
                    if name.strip()
                ]
                place_name = PlaceName()
                place_name.set_value(names[0])
                place.set_name(place_name)
                place.set_title('')

                parent_handle = None
                for name, handle in self.find_hierarchy(names)[:-1]:
                    if handle is None:
                        new_place = Place()
                        place_name = PlaceName()
                        place_name.set_value(name)
                        new_place.set_name(place_name)
                        if parent_handle is None:
                            new_place.set_type(PlaceType.COUNTRY)
                        if parent_handle is not None:
                            placeref = PlaceRef()
                            placeref.ref = parent_handle
                            new_place.add_placeref(placeref)
                        with DbTxn(_("Add Place"), self.db) as trans:
                            parent_handle = self.db.add_place(new_place, trans)
                    else:
                        parent_handle = handle

                if parent_handle is not None:
                    placeref = PlaceRef()
                    placeref.ref = parent_handle
                    place.add_placeref(placeref)
                with DbTxn(_("Edit Place"), self.db) as trans:
                    self.db.commit_place(place, trans)
Ejemplo n.º 2
0
    def __get_place(self, gov_id, type_dic, preferred_lang):
        gov_url = 'http://gov.genealogy.net/semanticWeb/about/' + quote(gov_id)

        response = urlopen(gov_url)
        data = response.read()

        dom = parseString(data)
        top = dom.getElementsByTagName('gov:GovObject')

        place = Place()
        place.gramps_id = gov_id
        if not len(top) :
            return place, []

        count = 0
        for element in top[0].getElementsByTagName('gov:hasName'):
            count += 1
            place_name = self.__get_hasname(element)
            if count == 1:
                place.set_name(place_name)
            else:
                if place_name.lang == preferred_lang:
                    place.add_alternative_name(place.get_name())
                    place.set_name(place_name)
                else:
                    place.add_alternative_name(place_name)
        for element in top[0].getElementsByTagName('gov:hasType'):
            curr_lang = place.get_name().get_language()
            place_type = self.__get_hastype(element,curr_lang, type_dic, preferred_lang)
            place.set_type(place_type)
        for element in top[0].getElementsByTagName('gov:position'):
            latitude, longitude = self.__get_position(element)
            place.set_latitude(latitude)
            place.set_longitude(longitude)
        ref_list = []
        for element in top[0].getElementsByTagName('gov:isPartOf'):
            ref, date = self.__get_ispartof(element)
            ref_list.append((ref, date))
        for element in top[0].getElementsByTagName('gov:isLocatedIn'):
            ref, date = self.__get_ispartof(element)
            ref_list.append((ref, date))
        for element in top[0].getElementsByTagName('gov:hasURL'):
            url = self.__get_hasurl(element)
            place.add_url(url)

        return place, ref_list
Ejemplo n.º 3
0
    def addPlace(pname, ptype, refPlace=None):
        place = Place()
        placeName = PlaceName()
        placeName.set_value(pname)
        place.set_name(placeName)
        #        place.set_change_time(chgtime)
        place.set_type(ptype)
        place.add_tag(tags[ptype])
        if refPlace != None:
            placeRef = PlaceRef()
            placeRef.set_reference_handle(refPlace.get_handle())
            place.add_placeref(placeRef)
#        tag.set_color("#EF2929")
        with DbTxn(_("Add Place"), db) as trans:
            phandle = db.add_place(place, trans)
            LOG.debug('Place added: ' + place.get_name().get_value() + ' ' +
                      phandle)
        return place
Ejemplo n.º 4
0
    def __get_place(self, gov_id, type_dic, preferred_lang):
        gov_url = 'http://gov.genealogy.net/semanticWeb/about/' + quote(gov_id)

        response = urlopen(gov_url)
        data = response.read()

        dom = parseString(data)
        top = dom.getElementsByTagName('gov:GovObject')

        place = Place()
        place.gramps_id = gov_id
        if not len(top) :
            return place, []

        count = 0
        for element in top[0].getElementsByTagName('gov:hasName'):
            count += 1
            place_name = self.__get_hasname(element)
            if count == 1:
                place.set_name(place_name)
            else:
                if place_name.lang == preferred_lang:
                    place.add_alternative_name(place.get_name())
                    place.set_name(place_name)
                else:
                    place.add_alternative_name(place_name)
        for element in top[0].getElementsByTagName('gov:hasType'):
            curr_lang = place.get_name().get_language()
            place_type = self.__get_hastype(element,curr_lang, type_dic, preferred_lang)
            place.set_type(place_type)
        for element in top[0].getElementsByTagName('gov:position'):
            latitude, longitude = self.__get_position(element)
            place.set_latitude(latitude)
            place.set_longitude(longitude)
        ref_list = []
        for element in top[0].getElementsByTagName('gov:isPartOf'):
            ref, date = self.__get_ispartof(element)
            ref_list.append((ref, date))
        for element in top[0].getElementsByTagName('gov:hasURL'):
            url = self.__get_hasurl(element)
            place.add_url(url)

        return place, ref_list
Ejemplo n.º 5
0
 def __addPlace(self, pname, ptype, altNames=None, refPlace=None, tag=None):
     place = Place()
     placeName = PlaceName() 
     placeName.set_value(pname)
     place.set_name(placeName)
     if altNames:
         place.set_alternative_names(altNames)                
     place.set_type(ptype)
     if tag:
         place.add_tag(tag)
     if refPlace:
         placeRef = PlaceRef()
         placeRef.set_reference_handle(refPlace.get_handle())
         place.add_placeref(placeRef)
     with DbTxn(_("Add Place"), self.dbstate.db) as trans:
         handle = self.dbstate.db.add_place(place, trans)
         place = self.dbstate.db.get_place_from_handle(handle)
         self.dbstate.db.commit_place(place, trans)
     return place 
Ejemplo n.º 6
0
    def addPlace(priName, altNames, ptype, refPlace=None):
        place = Place()
        placeName = PlaceName() 
        placeName.set_value(priName[0])
        placeName.set_language(priName[1])
        place.set_name(placeName)

        if len(altNames) > 0:
            place.set_alternative_names(altNames)                
#        place.set_change_time(chgtime)
        place.set_type(ptype)
#        place.add_tag(tags[ptype])
        place.add_tag(reftag)
        if refPlace != None:
            placeRef = PlaceRef()
            placeRef.set_reference_handle(refPlace.get_handle())
            place.add_placeref(placeRef)
#        tag.set_color("#EF2929")
        with DbTxn(_("Add Place"), db) as trans:
            phandle = db.add_place(place, trans)

            LOG.debug('Place added: ' + place.get_name().get_value() + ' ' + phandle)
        return place 
Ejemplo n.º 7
0
    def __addPlace(self, pname, ptype, altNames=None, refPlace=None, tag=None):
        place = Place()
        placeName = PlaceName()
        placeName.set_value(pname)
        #        placeName.set_language(priName[1])
        place.set_name(placeName)
        print(pname)
        if altNames:
            place.set_alternative_names(altNames)
#        place.set_change_time(chgtime)
        place.set_type(ptype)
        print(ptype)
        if tag:
            place.add_tag(tag)
        if refPlace:
            placeRef = PlaceRef()
            placeRef.set_reference_handle(refPlace.get_handle())
            place.add_placeref(placeRef)
        with DbTxn(_("Add Place"), self.dbstate.db) as trans:
            handle = self.dbstate.db.add_place(place, trans)
            place = self.dbstate.db.get_place_from_handle(handle)
            self.dbstate.db.commit_place(place, trans)
#            print('Place added: ' + place.get_name().get_value() + ' ' + phandle)
        return place
Ejemplo n.º 8
0
class PlaceSidebarFilter(SidebarFilter):

    def __init__(self, dbstate, uistate, clicked):
        self.clicked_func = clicked

        self.filter_id = widgets.BasicEntry()
        self.filter_name = widgets.BasicEntry()
        self.filter_place = Place()
        self.filter_place.set_type((PlaceType.CUSTOM, ''))
        self.ptype = Gtk.ComboBox(has_entry=True)
        if dbstate.is_open():
            self.custom_types = dbstate.db.get_place_types()
        else:
            self.custom_types = []

        self.place_menu = widgets.MonitoredDataType(
            self.ptype,
            self.filter_place.set_type,
            self.filter_place.get_type,
            False, # read-only
            self.custom_types
        )
        self.filter_code = widgets.BasicEntry()
        self.filter_enclosed = widgets.PlaceEntry(dbstate, uistate, [])
        self.filter_note = widgets.BasicEntry()

        self.filter_regex = Gtk.CheckButton(label=_('Use regular expressions'))
        self.tag = Gtk.ComboBox()
        self.generic = Gtk.ComboBox()

        SidebarFilter.__init__(self, dbstate, uistate, "Place")

    def create_widget(self):
        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.generic.pack_start(cell, True)
        self.generic.add_attribute(cell, 'text', 0)
        self.on_filters_changed('Place')

        cell = Gtk.CellRendererText()
        cell.set_property('width', self._FILTER_WIDTH)
        cell.set_property('ellipsize', self._FILTER_ELLIPSIZE)
        self.tag.pack_start(cell, True)
        self.tag.add_attribute(cell, 'text', 0)

        self.add_text_entry(_('ID'), self.filter_id)
        self.add_text_entry(_('Name'), self.filter_name)
        self.add_entry(_('Type'), self.ptype)
        self.add_text_entry(_('Code'), self.filter_code)
        self.add_text_entry(_('Enclosed By'), self.filter_enclosed)
        self.add_text_entry(_('Note'), self.filter_note)
        self.add_entry(_('Tag'), self.tag)
        self.add_filter_entry(_('Custom filter'), self.generic)
        self.add_regex_entry(self.filter_regex)

    def clear(self, obj):
        self.filter_id.set_text('')
        self.filter_name.set_text('')
        self.filter_code.set_text('')
        self.filter_enclosed.set_text('')
        self.filter_note.set_text('')
        self.ptype.get_child().set_text('')
        self.tag.set_active(0)
        self.generic.set_active(0)

    def get_filter(self):
        gid = str(self.filter_id.get_text()).strip()
        name = str(self.filter_name.get_text()).strip()
        ptype = self.filter_place.get_type().xml_str()
        code = str(self.filter_code.get_text()).strip()
        enclosed = str(self.filter_enclosed.get_text()).strip()
        note = str(self.filter_note.get_text()).strip()
        regex = self.filter_regex.get_active()
        tag = self.tag.get_active() > 0
        gen = self.generic.get_active() > 0

        empty = not (gid or name or ptype or code or enclosed or note or regex
                     or tag or gen)
        if empty:
            generic_filter = None
        else:
            generic_filter = GenericPlaceFilter()
            if gid:
                rule = RegExpIdOf([gid], use_regex=regex)
                generic_filter.add_rule(rule)

            if enclosed:
                rule = IsEnclosedBy([enclosed, '0'])
                generic_filter.add_rule(rule)

            rule = HasData([name, ptype, code], use_regex=regex)
            generic_filter.add_rule(rule)

            if note:
                rule = HasNoteRegexp([note], use_regex=regex)
                generic_filter.add_rule(rule)

            # check the Tag
            if tag:
                model = self.tag.get_model()
                node = self.tag.get_active_iter()
                attr = model.get_value(node, 0)
                rule = HasTag([attr])
                generic_filter.add_rule(rule)

            if self.generic.get_active() != 0:
                model = self.generic.get_model()
                node = self.generic.get_active_iter()
                obj = str(model.get_value(node, 0))
                rule = MatchesFilter([obj])
                generic_filter.add_rule(rule)

        return generic_filter

    def on_filters_changed(self, name_space):
        if name_space == 'Place':
            all_filter = GenericPlaceFilter()
            all_filter.set_name(_("None"))
            all_filter.add_rule(rules.place.AllPlaces([]))
            self.generic.set_model(build_filter_model('Place', [all_filter]))
            self.generic.set_active(0)

    def on_tags_changed(self, tag_list):
        """
        Update the list of tags in the tag filter.
        """
        model = Gtk.ListStore(str)
        model.append(('',))
        for tag_name in tag_list:
            model.append((tag_name,))
        self.tag.set_model(model)
        self.tag.set_active(0)
Ejemplo n.º 9
0
def get_place_from_wikidata(entity_id):
    parents = set()
    entity = WikidataItem(get_entity_dict_from_api(entity_id))
    claims_groups = entity.get_truthy_claim_groups()
    place = Place()
    place.set_gramps_id(entity_id)

    name = PlaceName()
    name.set_language('sv')
    name.set_value(entity.get_label('sv'))
    place.set_name(name=name)

    place.set_title(entity.get_label('sv'))
    for lang in ['sv', 'en', 'de', 'fi', 'no', 'nn', 'da', 'se']:
        wiki_name = entity.get_label(lang)
        if len(wiki_name):
            place_name = PlaceName()
            place_name.set_language(lang)
            place_name.set_value(wiki_name)
            place.add_alternative_name(name=place_name)
            for alias in entity.get_aliases(lang):
                alt_name = PlaceName()
                alt_name.set_language(lang)
                alt_name.set_value(alias)
                place.add_alternative_name(name=alt_name)

        for link in entity.get_sitelinks(lang).values():
            wikipedia_url = Url()
            wikipedia_url.set_path(link['url'])
            wikipedia_url.set_type('Wikipedia entry')
            wikipedia_url.set_description('Wikipedia %s:%s' %
                                          (link["title"], link["site"]))
            place.add_url(wikipedia_url)

    # Instance of -> PlaceType
    if PROPERTY_INSTANCE_OF in claims_groups:
        for claim in claims_groups[PROPERTY_INSTANCE_OF]:
            instance_of = claim.mainsnak.datavalue.value['id']
            if ITEM_PARISH == instance_of:
                place.set_type(PlaceType.PARISH)
            elif ITEM_SOCKEN == instance_of:
                place.set_type(PlaceType.PARISH)
            elif ITEM_ISLAND == instance_of:
                place.set_type(PlaceType.UNKNOWN)  # No islands in Gramps
            elif ITEM_MUNICIPALITY_OF_SWEDEN == instance_of:
                place.set_type(PlaceType.MUNICIPALITY)
            elif ITEM_MUNICIPALITY == instance_of:
                place.set_type(PlaceType.MUNICIPALITY)
            elif ITEM_COUNTRY == instance_of:
                place.set_type(PlaceType.COUNTRY)
            elif ITEM_SOVEREIGN_STATE == instance_of:
                place.set_type(PlaceType.COUNTRY)
            elif ITEM_STATE_OF_US == instance_of:
                place.set_type(PlaceType.STATE)
            elif ITEM_FEDERAL_STATE == instance_of:
                place.set_type(PlaceType.STATE)
            elif ITEM_COUNTY == instance_of:
                place.set_type(PlaceType.COUNTY)
            elif ITEM_COUNTY_OF_SWEDEN == instance_of:
                place.set_type(PlaceType.COUNTY)
            elif ITEM_FORMER_COUNTY_OF_SWEDEN == instance_of:
                place.set_type(PlaceType.COUNTY)
            elif ITEM_PROVINCE_OF_SWEDEN == instance_of:
                place.set_type(PlaceType.PROVINCE)
            elif ITEM_PROVINCE == instance_of:
                place.set_type(PlaceType.PROVINCE)
            elif ITEM_ADM_REGION == instance_of:
                place.set_type(PlaceType.REGION)
            elif ITEM_NEIGHBORHOOD == instance_of:
                place.set_type(PlaceType.NEIGHBORHOOD)
            elif ITEM_DISTRICT == instance_of:
                place.set_type(PlaceType.DISTRICT)
            elif ITEM_BOROUGH == instance_of:
                place.set_type(PlaceType.BOROUGH)
            elif ITEM_TOWN == instance_of:
                place.set_type(PlaceType.TOWN)
            elif ITEM_LARGE_VILLAGE == instance_of:
                place.set_type(PlaceType.VILLAGE)
            elif ITEM_VILLAGE == instance_of:
                place.set_type(PlaceType.VILLAGE)
            elif ITEM_URBAN_AREA_IN_SWEDEN == instance_of:
                place.set_type(PlaceType.VILLAGE)
            elif ITEM_HAMLET == instance_of:
                place.set_type(PlaceType.HAMLET)
            elif ITEM_FARM == instance_of:
                place.set_type(PlaceType.FARM)
            elif ITEM_BUILDING == instance_of:
                place.set_type(PlaceType.BUILDING)

    if PROPERTY_COORDINATE_LOCATION in claims_groups:
        for claim in claims_groups[PROPERTY_COORDINATE_LOCATION]:
            datavalue = claim.mainsnak.datavalue
            place.set_latitude(str(datavalue.value['latitude']))
            place.set_longitude(str(datavalue.value['longitude']))

    extract_located_in(claims_groups, PROPERTY_LOCATED_IN_PRESENT, parents)
    extract_located_in(claims_groups, PROPERTY_LOCATED_IN_ADM, parents)
    extract_located_in(claims_groups, PROPERTY_LOCATED, parents)

    return place, parents