Ejemplo n.º 1
0
    def __generate_hierarchy(self, place, original_enclosing_places):
        # Generates the hierarchy.
        # Returns the place at the top of the hierarchy or None if no hierarchy was generated
        original_name = place.get_name().get_value()
        if original_name == "":
            original_name = place.get_title()
        separator = ','
        if self.spaces.get_active():
            separator = None
            if ' ' not in original_name:
                return None
        else:
            if ',' not in original_name:
                return None

        names = [
            name.strip() for name in original_name.split(separator)
            if name.strip()
        ]
        if self.reverse.get_active():
            names.reverse()
        place_name = PlaceName()
        place_name.set_value(names[0])
        place.set_name(place_name)
        place.set_title('')

        parent_handle = None
        top_place = place
        for name, handle, new_place in self.find_hierarchy(
                names, original_enclosing_places)[:-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:
                    top_place = new_place
                if parent_handle is not None:
                    placeref = PlaceRef()
                    placeref.ref = parent_handle
                    placeref.set_date_object(self.date_object)
                    new_place.add_placeref(placeref)
                parent_handle = self.dbstate.db.add_place(
                    new_place, self.trans)
            else:
                if parent_handle is None:
                    top_place = new_place
                parent_handle = handle

        if parent_handle is not None:
            placeref = PlaceRef()
            placeref.ref = parent_handle
            placeref.set_date_object(self.date_object)
            place.set_placeref_list([placeref
                                     ])  # this removes any previous parent
        return top_place
Ejemplo n.º 2
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.º 3
0
    def add_place_from_kml(self, menu, event, lat, lon):
        """
        Add new place(s) from a kml file

        1 - ask for a kml file ?
        2 - Read the kml file.
        3 - create the place(s) with name and title found in the kml marker.

        """
        dummy_menu = menu
        dummy_event = event
        dummy_lat = lat
        dummy_lon = lon
        # Ask for the kml file
        filtering = Gtk.FileFilter()
        filtering.add_pattern("*.kml")
        kml = Gtk.FileChooserDialog(_("Select a kml file used to add places"),
                                    action=Gtk.FileChooserAction.OPEN,
                                    parent=self.uistate.window,
                                    buttons=(_('_Cancel'),
                                             Gtk.ResponseType.CANCEL,
                                             _('_Apply'), Gtk.ResponseType.OK))
        mpath = HOME_DIR
        kml.set_current_folder(os.path.dirname(mpath))
        kml.set_filter(filtering)

        status = kml.run()
        if status == Gtk.ResponseType.OK:
            val = kml.get_filename()
            if val:
                kmlfile = Kml(val)
                points = kmlfile.add_points()
                for place in points:
                    (name, coords) = place
                    latlong = coords.pop()
                    (lat, lon) = latlong
                    place_name = PlaceName()
                    place_name.set_value(name)
                    new_place = Place()
                    new_place.set_name(place_name)
                    new_place.set_title(name)
                    new_place.set_latitude(str(lat))
                    new_place.set_longitude(str(lon))
                    try:
                        EditPlace(self.dbstate, self.uistate, [], new_place)
                    except WindowActiveError:
                        pass
        kml.destroy()
Ejemplo n.º 4
0
    def __execute(self, obj):
        with DbTxn(_("Processing marriages"), self.dbstate.db) as self.trans:
            selected_handles = self.uistate.viewmanager.active_page.selected_handles(
            )
            num_places = len(selected_handles)
            for eventhandle in selected_handles:
                event = self.dbstate.db.get_event_from_handle(eventhandle)
                print(event)
                if not event.get_type().is_marriage(): continue
                placehandle = event.get_place_handle()
                place = self.dbstate.db.get_place_from_handle(placehandle)
                pname = place.get_name().value
                places = self.__match2(pname)
                if not places: continue
                print(places)
                place1, husb_place, wife_place = places
                family_handles = list(
                    self.dbstate.db.find_backlink_handles(
                        eventhandle, ['Family']))
                print(family_handles)
                for objtype, family_handle in family_handles:
                    family = self.dbstate.db.get_family_from_handle(
                        family_handle)
                    father_handle = family.get_father_handle()
                    mother_handle = family.get_mother_handle()
                    if not father_handle or not mother_handle: continue
                    father = self.dbstate.db.get_person_from_handle(
                        father_handle)
                    father_name = father.get_primary_name().get_name()
                    mother = self.dbstate.db.get_person_from_handle(
                        mother_handle)
                    mother_name = mother.get_primary_name().get_name()
                    print(father_name, mother_name)
                    self.__add_resi_event(father, husb_place,
                                          event.get_date_object())
                    self.__add_resi_event(mother, wife_place,
                                          event.get_date_object())
                    pn = PlaceName()
                    pn.set_value(place1)
                    newplace = Place()
                    newplace.set_name(pn)
                    newplacehandle = self.dbstate.db.add_place(
                        newplace, self.trans)
                    event.set_place_handle(newplacehandle)

                event.set_description("marriage")
                self.dbstate.db.commit_event(event, self.trans)
                self.dbstate.db.commit_place(place, self.trans)
Ejemplo n.º 5
0
 def __add_place(self, parent, plat, plon):
     """
     Add a new place using longitude and latitude of location centered
     on the map
     """
     self.select_fct.close()
     new_place = Place()
     new_place.set_latitude(str(plat))
     new_place.set_longitude(str(plon))
     if parent:
         if isinstance(parent, Place):
             placeref = PlaceRef()
             placeref.ref = parent
             new_place.add_placeref(placeref)
         elif isinstance(parent, gi.overrides.Gtk.TreeModelRow):
             # We are here because we selected a place from geocoding
             # parent[0] : country
             # parent[1] : state
             # parent[2] : town
             # parent[3] : name
             value = PlaceSelection.untag_text(parent[2], 1)
             plname = PlaceName()
             plname.set_value(value)
             handle = self.place_exists(value)
             if handle:
                 # The town already exists. We create a place with name
                 placeref = PlaceRef()
                 placeref.ref = handle
                 new_place.add_placeref(placeref)
                 value = PlaceSelection.untag_text(parent[3], 1)
                 plname.set_value(value)
             new_place.set_name(plname)
         else:
             found = None
             for place in self.dbstate.db.iter_places():
                 found = place
                 if place.name.get_value() == parent:
                     break
             placeref = PlaceRef()
             placeref.ref = found.get_handle()
             new_place.add_placeref(placeref)
     try:
         EditPlace(self.dbstate, self.uistate, [], new_place)
         self.add_marker(None, None, plat, plon, None, True, 0)
     except WindowActiveError:
         pass
Ejemplo n.º 6
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.º 7
0
 def __add_place(self, parent, plat, plon):
     """
     Add a new place using longitude and latitude of location centered
     on the map
     """
     self.select_fct.close()
     new_place = Place()
     new_place.set_latitude(str(plat))
     new_place.set_longitude(str(plon))
     if parent:
         if isinstance(parent, Place):
             placeref = PlaceRef()
             placeref.ref = parent
             new_place.add_placeref(placeref)
         elif isinstance(parent, gi.overrides.Gtk.TreeModelRow):
             # We are here because we selected a place from geocoding
             # parent[0] : country
             # parent[1] : state
             # parent[2] : town
             # parent[3] : name
             value = PlaceSelection.untag_text(parent[2], 1)
             plname = PlaceName()
             plname.set_value(value)
             handle = self.place_exists(value)
             if handle:
                 # The town already exists. We create a place with name
                 placeref = PlaceRef()
                 placeref.ref = handle
                 new_place.add_placeref(placeref)
                 value = PlaceSelection.untag_text(parent[3], 1)
                 plname.set_value(value)
             new_place.set_name(plname)
         else:
             found = None
             for place in self.dbstate.db.iter_places():
                 found = place
                 if place.name.get_value() == parent:
                     break
             placeref = PlaceRef()
             placeref.ref = found.get_handle()
             new_place.add_placeref(placeref)
     try:
         EditPlace(self.dbstate, self.uistate, [], new_place)
         self.add_marker(None, None, plat, plon, None, True, 0)
     except WindowActiveError:
         pass
Ejemplo n.º 8
0
    def __add_resi_event(self, person, placename, date_object):
        pn = PlaceName()
        pn.set_value(placename)
        place = Place()
        place.set_name(pn)
        placehandle = self.dbstate.db.add_place(place, self.trans)

        e = Event()
        e.set_type(EventType.RESIDENCE)
        e.set_date_object(date_object)
        e.set_place_handle(placehandle)
        e.set_description("marriage")
        eventhandle = self.dbstate.db.add_event(e, self.trans)

        eref = EventRef()
        eref.ref = eventhandle
        person.add_event_ref(eref)
        self.dbstate.db.commit_person(person, self.trans)
Ejemplo n.º 9
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.º 10
0
    def add_place_from_kml(self, menu, event, lat, lon):
        """
        Add new place(s) from a kml file

        1 - ask for a kml file ?
        2 - Read the kml file.
        3 - create the place(s) with name and title found in the kml marker.

        """
        # Ask for the kml file
        filtering = Gtk.FileFilter()
        filtering.add_pattern("*.kml")
        kml = Gtk.FileChooserDialog(
            _("Select a kml file used to add places"),
            action=Gtk.FileChooserAction.OPEN,
            parent=self.uistate.window,
            buttons=(_('_Cancel'), Gtk.ResponseType.CANCEL,
                     _('_Apply'), Gtk.ResponseType.OK))
        mpath = HOME_DIR
        kml.set_current_folder(os.path.dirname(mpath))
        kml.set_filter(filtering)

        status = kml.run()
        if status == Gtk.ResponseType.OK:
            val = kml.get_filename()
            if val:
                kmlfile = Kml(val)
                points = kmlfile.add_points()
                for place in points:
                    (name, coords) = place
                    latlong = coords.pop()
                    (lat, lon) = latlong
                    place_name = PlaceName()
                    place_name.set_value(name)
                    new_place = Place()
                    new_place.set_name(place_name)
                    new_place.set_title(name)
                    new_place.set_latitude(str(lat))
                    new_place.set_longitude(str(lon))
                    try:
                        EditPlace(self.dbstate, self.uistate, [], new_place)
                    except WindowActiveError:
                        pass
        kml.destroy()
Ejemplo n.º 11
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.º 12
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.º 13
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.º 14
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.º 15
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