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)
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()
def on_ok_clicked(self, obj): with DbTxn(_("Extract Place data"), self.db, batch=True) as self.trans: self.db.disable_signals() changelist = [ node for node in self.iter_list if self.model.get_value(node, 0) ] for change in changelist: row = self.model[change] place = self.db.get_place_from_handle(row[6]) location = ('', '', '', row[2], '', row[3], row[5]) self.place_import.store_location(location, place.handle) if row[2]: place.set_name(PlaceName(value=row[2])) place.set_type(PlaceType.CITY) if row[4]: place.set_code(row[4]) self.db.commit_place(place, self.trans) self.place_import.generate_hierarchy(self.trans) self.db.enable_signals() self.db.request_rebuild() self.close()
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
def __add_place(self, name, type_num, parent, title, trans): """ Add a missing place to the database. """ place = Place() place_name = PlaceName() place_name.set_value(name) place.name = place_name place.title = title place.place_type = PlaceType(7-type_num) if parent is not None: placeref = PlaceRef() placeref.ref = handle2internal(parent) place.set_placeref_list([placeref]) handle = self.db.add_place(place, trans) self.db.commit_place(place, trans) return handle
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
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
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)
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()
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
def __get_hasname(self, element): name = PlaceName() pname = element.getElementsByTagName('gov:PropertyName') if len(pname): value = pname[0].getElementsByTagName('gov:value') if len(value): name.set_value(value[0].childNodes[0].data) language = pname[0].getElementsByTagName('gov:language') if len(language): name.set_language(ISO_CODE_LOOKUP.get(language[0].childNodes[0].data)) date = self.__get_date_range(pname[0]) name.set_date_object(date) return name
def add_button_clicked(self, obj): """ Called when the Add button is clicked. """ pname = PlaceName() try: from .. import EditPlaceName EditPlaceName(self.dbstate, self.uistate, self.track, pname, self.add_callback) except WindowActiveError: return
def parseNames(pname, plang='fi'): pnames = pname.split(',') LOG.debug('Place name %s split into %d pieces' % (pname, len(pnames))) priName = (pnames[0].strip(), plang) altNames = [] if len(pnames) > 1: del pnames[0] for aname in pnames: altName = PlaceName() anameparts = aname.split(':') if len(anameparts) == 1: altName.set_value(anameparts[0].strip()) altName.set_language('se') altNames.append(altName) elif len(anameparts) == 2: altName.set_value(anameparts[1].strip()) altName.set_language(anameparts[0].strip()) altNames.append(altName) else: LOG.error('Pieleen meni? %s' % aname) return priName, altNames
def on_edit_clicked(self, dummy): """User wants to jump directly to the results view to finish off the place, possibly because a place was not found""" # if ',' in self.place.name.value: # name = self.place.name.value # else: name = self.place.name.value self.newplace = NewPlace(name) names = name.split(',') names = [name.strip() for name in names] self.newplace.name = PlaceName() self.newplace.name.value = names[0] self.newplace.gramps_id = self.place.gramps_id self.newplace.lat = self.place.lat self.newplace.long = self.place.long self.newplace.code = self.place.code if self.place.place_type == PlaceType.UNKNOWN: self.newplace.place_type = PlaceType(PlaceType.UNKNOWN) if any(i.isdigit() for i in self.newplace.name.value): self.newplace.place_type = PlaceType(PlaceType.STREET) ptype = PlaceType() for tname in self.newplace.name.value.split(' '): # see if it is an English PlaceType ptype.set_from_xml_str(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is a translated PlaceType ptype.set(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is an already added custom type cust_types = self.dbstate.db.get_place_types() if tname.capitalize() in cust_types: self.newplace.place_type = ptype else: self.newplace.place_type = self.place.place_type self.newplace.add_name(self.newplace.name) self.newplace.add_name(self.place.name) self.newplace.add_names(self.place.alt_names) if self.place.placeref_list: # If we already have an enclosing place, use it. parent = self.dbstate.db.get_place_from_handle( self.place.placeref_list[0].ref) self.newplace.parent_ids = [parent.gramps_id] elif len(names) > 1: # we have an enclosing place, according to the name string self.newplace.parent_names = names[1:] self.gui.get_child().remove(self.mainwin) self.gui.get_child().add(self.results_win) self.res_gui()
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)
def get_or_create_place(self, place_name): "Return the requested place object tuple-packed with a new indicator." LOG.debug("get_or_create_place: looking for: %s", place_name) for place_handle in self.db.iter_place_handles(): place = self.db.get_place_from_handle(place_handle) place_title = place_displayer.display(self.db, place) if place_title == place_name: return (0, place) place = Place() place.set_title(place_name) place.name = PlaceName(value=place_name) self.db.add_place(place, self.trans) return (1, place)
def __init__(self, title): self.title = title self.gramps_id = '' self.lat = '' self.long = '' self.code = '' self.place_type = None self.names = [] # all names, including alternate, acts like a set self.name = PlaceName() self.links = [] self.geoid = '' self.parent_ids = [] # list of gramps_ids in hierarchical order self.parent_names = [] # list of names in hierarchical order
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
def _parse_place(self, line_number, row, col): "Parse the content of a Place line." place_id = rd(line_number, row, col, "place") place_title = rd(line_number, row, col, "title") place_name = rd(line_number, row, col, "name") place_type_str = rd(line_number, row, col, "type") place_latitude = rd(line_number, row, col, "latitude") place_longitude = rd(line_number, row, col, "longitude") place_code = rd(line_number, row, col, "code") place_enclosed_by_id = rd(line_number, row, col, "enclosed_by") place_date = rd(line_number, row, col, "date") ######################################################### # if this place already exists, don't create it place = self.lookup("place", place_id) if place is None: # new place place = self.create_place() if place_id is not None: if place_id.startswith("[") and place_id.endswith("]"): place.gramps_id = self.db.pid2user_format(place_id[1:-1]) self.storeup("place", place_id, place) if place_title is not None: place.title = place_title if place_name is not None: place.name = PlaceName(value=place_name) if place_type_str is not None: place.place_type = self.get_place_type(place_type_str) if place_latitude is not None: place.lat = place_latitude if place_longitude is not None: place.long = place_longitude if place_code is not None: place.code = place_code if place_enclosed_by_id is not None: place_enclosed_by = self.lookup("place", place_enclosed_by_id) if place_enclosed_by is None: raise Exception("cannot enclose %s in %s as it doesn't exist" % (place.gramps_id, place_enclosed_by_id)) if not place_enclosed_by.handle in place.placeref_list: placeref = PlaceRef() placeref.ref = place_enclosed_by.handle if place_date: placeref.date = _dp.parse(place_date) place.placeref_list.append(placeref) ######################################################### self.db.commit_place(place, self.trans)
def set_info(self,place,info): placename = PlaceName() names = info["names"] ptype = info["type"] tags = info["tags"] primary_name = names[0]["name"] primary_name_lang = names[0]["lang"] placename.set_value(primary_name) placename.set_language(primary_name_lang) place.set_name(placename) place.set_title('') # ?? self.set_type(place,ptype) self.set_altnames(place,names[1:]) if tags: print("tag:",primary_name,tags) for tagdata in tags: taghandle = self.find_tag(tagdata) place.add_tag(taghandle)
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
def on_res_ok_clicked(self, dummy): """ Accept changes displayed and commit to place. Also find or create a new enclosing place from parent. """ # do the names namelist = [] for row in self.alt_store: if row[0] == 'P': self.place.name = self.newplace.names[row[4]] elif row[0] == '\u2714': namelist.append(self.newplace.names[row[4]]) self.place.alt_names = namelist # Lat/lon/ID/code/type self.place.lat = self.top.get_object('latitude').get_text() self.place.long = self.top.get_object('longitude').get_text() self.place.gramps_id = self.top.get_object('grampsid').get_text() self.place.code = self.top.get_object('postal').get_text() self.place.place_type.set(self.type_combo.get_values()) # Add in URLs if wanted if self.keepweb: for url in self.newplace.links: self.place.add_url(url) # Enclose in the next level place next_place = False parent = None if not self.keep_enclosure or not self.place.placeref_list: if self.newplace.parent_ids: # we might have a parent with geo id 'GEO12345' parent = self.dbstate.db.get_place_from_gramps_id( self.newplace.parent_ids[0]) if not parent and self.newplace.parent_names: # make one, will have to be examined/cleaned later parent = Place() parent.title = ', '.join(self.newplace.parent_names) name = PlaceName() name.value = parent.title parent.name = name parent.gramps_id = self.newplace.parent_ids[0] with DbTxn( _("Add Place (%s)") % parent.title, self.dbstate.db) as trans: self.dbstate.db.add_place(parent, trans) next_place = True if parent: if located_in(self.dbstate.db, parent.handle, self.place.handle): # attempting to create a place loop, not good! ErrorDialog(_('Place cycle detected'), msg2=_("The place you chose is enclosed in the" " place you are workin on!\n" "Please cancel and choose another " "place."), parent=self.uistate.window) return # check to see if we already have the enclosing place already_there = False for pref in self.place.placeref_list: if parent.handle == pref.ref: already_there = True break if not already_there: placeref = PlaceRef() placeref.set_reference_handle(parent.handle) self.place.set_placeref_list([placeref]) # Add in Citation/Source if wanted if self.addcitation and self.newplace.geoid: src_hndl = self.find_or_make_source() cit = Citation() cit.set_reference_handle(src_hndl) cit.set_page("GeoNames ID: %s" % self.newplace.geoid.replace('GEO', '')) with DbTxn(_("Add Citation (%s)") % "GeoNames", self.dbstate.db) as trans: self.dbstate.db.add_citation(cit, trans) self.place.add_citation(cit.handle) # We're finally ready to commit the updated place with DbTxn(_("Edit Place (%s)") % self.place.title, self.dbstate.db) as trans: self.dbstate.db.commit_place(self.place, trans) # Jump to enclosing place to clean it if necessary if next_place: self.set_active('Place', parent.handle) self.place = parent # if geoparse fails, leave us at main view if self.newplace.parent_ids and \ self.geoparse(self.newplace.parent_ids[0], _(", ").join(self.newplace.parent_names), self.newplace.parent_ids, self.newplace.parent_names): # geoparse worked, lets put up the results view self.gui.get_child().remove(self.mainwin) self.gui.get_child().add(self.results_win) self.res_gui() return self.reset_main() if self.gui.get_child().get_child() == self.results_win: self.gui.get_child().remove(self.results_win) self.gui.get_child().add(self.mainwin)
def geoparse(self, geoid, title, h_geoid_list, h_name_list, *dummy): """ get data for place and parse out g_name dom structure into the NewPlace structure """ geo_url = ('http://api.geonames.org/get?geonameId=%s&style=FULL' '&username=%s' % (geoid.replace('GEO', ''), self.geonames_id)) dom = self.get_geo_data(geo_url) if not dom: return False g_name = dom.getElementsByTagName('geoname')[0] self.newplace = NewPlace(title) self.newplace.geoid = geoid self.newplace.gramps_id = geoid value = g_name.getElementsByTagName('lat') self.newplace.lat = str(value[0].childNodes[0].data) value = g_name.getElementsByTagName('lng') self.newplace.long = str(value[0].childNodes[0].data) value = g_name.getElementsByTagName('toponymName') topname = value[0].childNodes[0].data new_place = PlaceName() new_place.set_value(topname) new_place.set_language("") # make sure we have the topname in the names list and default to # primary self.newplace.add_name(new_place) self.newplace.name = new_place # lets parse the alternative names alt_names = g_name.getElementsByTagName('alternateName') for a_name in alt_names: pattr = a_name.getAttribute("lang") value = a_name.childNodes[0].data if pattr == "post": if self.newplace.code: self.newplace.code += " " + value else: self.newplace.code = value elif pattr == "link": url = Url() url.set_path(value) url.set_description(value) url.set_type(UrlType(UrlType.WEB_HOME)) self.newplace.links.append(url) elif pattr not in ['iata', 'iaco', 'faac', 'wkdt', 'unlc']: new_place = PlaceName() new_place.set_language(pattr) new_place.set_value(value) self.newplace.add_name(new_place) if a_name.hasAttribute('isPreferredName') and ( pattr and pattr == self.lang): # if not preferred lang, we use topo name, otherwise # preferred name for lang self.newplace.name = new_place # Try to deduce PlaceType: # If populated place, set as City. Long description could over-ride # Parse long description, looking for keyword (Region, County, ...) # Top-level must be a country. # Children of USA are States. # Children of Canada are Provinces. # value = g_name.getElementsByTagName('fcl') fcl = value[0].childNodes[0].data value = g_name.getElementsByTagName('fcode') fcode = value[0].childNodes[0].data value = g_name.getElementsByTagName('countryCode') countrycode = value[0].childNodes[0].data self.newplace.place_type = PlaceType(PlaceType.UNKNOWN) ptype = PlaceType() # scan thorough names looking for name portion that matches a Placetype for name in self.newplace.names: for tname in name.value.split(' '): ptype.set_from_xml_str(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is a translated PlaceType ptype.set(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is an already added custom type cust_types = self.dbstate.db.get_place_types() if tname.capitalize() in cust_types: self.newplace.place_type = ptype break else: # Continue if the inner loop wasn't broken. continue # Inner loop was broken, break the outer. break if fcl == 'P': self.newplace.place_type = PlaceType(PlaceType.CITY) elif fcode == 'PRSH': self.newplace.place_type = PlaceType(PlaceType.PARISH) elif 'PCL' in fcode: self.newplace.place_type = PlaceType(PlaceType.COUNTRY) elif 'ADM' in fcode: if countrycode in self.adm_table: _ptype = self.adm_table[countrycode].get(fcode[:4]) if _ptype and (_ptype[1] or self.newplace.place_type.is_default()): self.newplace.place_type = PlaceType(_ptype[0]) # save a parent for enclosing if len(h_geoid_list) > 1: # we have a parent self.newplace.parent_names = h_name_list[1:] self.newplace.parent_ids = h_geoid_list[1:] return True
def set_altnames(self, place, names): for pn in names: place_name = PlaceName() place_name.set_value(pn["name"]) place_name.set_language(pn["lang"]) place.add_alternative_name(place_name)
def on_res_ok_clicked(self, dummy): """ Accept changes displayed and commit to place. Also find or create a new enclosing place from parent. """ # do the names namelist = [] for row in self.alt_store: if row[0] == 'P': self.place.name = self.newplace.names[row[4]] elif row[0] == '\u2714': namelist.append(self.newplace.names[row[4]]) self.place.alt_names = namelist # Lat/lon/ID/code/type self.place.lat = self.top.get_object('latitude').get_text() self.place.long = self.top.get_object('longitude').get_text() self.place.gramps_id = self.top.get_object('grampsid').get_text() self.place.code = self.top.get_object('postal').get_text() self.place.place_type.set(self.type_combo.get_values()) # Add in URLs if wanted if self.keepweb: for url in self.newplace.links: self.place.add_url(url) # Enclose in the next level place next_place = False parent = None if not self.keep_enclosure or not self.place.placeref_list: if self.newplace.parent_ids: # we might have a parent with geo id 'GEO12345' parent = self.dbstate.db.get_place_from_gramps_id( self.newplace.parent_ids[0]) if not parent and self.newplace.parent_names: # make one, will have to be examined/cleaned later parent = Place() parent.title = ', '.join(self.newplace.parent_names) name = PlaceName() name.value = parent.title parent.name = name parent.gramps_id = self.newplace.parent_ids[0] with DbTxn(_("Add Place (%s)") % parent.title, self.dbstate.db) as trans: self.dbstate.db.add_place(parent, trans) next_place = True if parent: if located_in(self.dbstate.db, parent.handle, self.place.handle): # attempting to create a place loop, not good! ErrorDialog(_('Place cycle detected'), msg2=_("The place you chose is enclosed in the" " place you are workin on!\n" "Please cancel and choose another " "place."), parent=self.uistate.window) return # check to see if we already have the enclosing place already_there = False for pref in self.place.placeref_list: if parent.handle == pref.ref: already_there = True break if not already_there: placeref = PlaceRef() placeref.set_reference_handle(parent.handle) self.place.set_placeref_list([placeref]) # Add in Citation/Source if wanted if self.addcitation and self.newplace.geoid: src_hndl = self.find_or_make_source() cit = Citation() cit.set_reference_handle(src_hndl) cit.set_page("GeoNames ID: %s" % self.newplace.geoid.replace('GEO', '')) with DbTxn(_("Add Citation (%s)") % "GeoNames", self.dbstate.db) as trans: self.dbstate.db.add_citation(cit, trans) self.place.add_citation(cit.handle) # We're finally ready to commit the updated place with DbTxn(_("Edit Place (%s)") % self.place.title, self.dbstate.db) as trans: self.dbstate.db.commit_place(self.place, trans) # Jump to enclosing place to clean it if necessary if next_place: self.set_active('Place', parent.handle) self.place = parent # if geoparse fails, leave us at main view if self.newplace.parent_ids and \ self.geoparse(self.newplace.parent_ids[0], _(", ").join(self.newplace.parent_names), self.newplace.parent_ids, self.newplace.parent_names): # geoparse worked, lets put up the results view self.gui.get_child().remove(self.mainwin) self.gui.get_child().add(self.results_win) self.res_gui() return self.reset_main() if self.gui.get_child().get_child() == self.results_win: self.gui.get_child().remove(self.results_win) self.gui.get_child().add(self.mainwin)
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
def geoparse(self, geoid, title, h_geoid_list, h_name_list, *dummy): """ get data for place and parse out g_name dom structure into the NewPlace structure """ geo_url = ('http://api.geonames.org/get?geonameId=%s&style=FULL' '&username=%s' % (geoid.replace('GEO', ''), self.geonames_id)) dom = self.get_geo_data(geo_url) if not dom: return False g_name = dom.getElementsByTagName('geoname')[0] self.newplace = NewPlace(title) self.newplace.geoid = geoid self.newplace.gramps_id = geoid value = g_name.getElementsByTagName('lat') self.newplace.lat = str(value[0].childNodes[0].data) value = g_name.getElementsByTagName('lng') self.newplace.long = str(value[0].childNodes[0].data) value = g_name.getElementsByTagName('toponymName') topname = value[0].childNodes[0].data new_place = PlaceName() new_place.set_value(topname) new_place.set_language("") # make sure we have the topname in the names list and default to # primary self.newplace.add_name(new_place) self.newplace.name = new_place # lets parse the alternative names alt_names = g_name.getElementsByTagName('alternateName') for a_name in alt_names: pattr = a_name.getAttribute("lang") value = a_name.childNodes[0].data if pattr == "post": if self.newplace.code: self.newplace.code += " " + value else: self.newplace.code = value elif pattr == "link": url = Url() url.set_path(value) url.set_description(value) url.set_type(UrlType(UrlType.WEB_HOME)) self.newplace.links.append(url) elif pattr not in ['iata', 'iaco', 'faac', 'wkdt', 'unlc']: new_place = PlaceName() new_place.set_language(pattr) new_place.set_value(value) self.newplace.add_name(new_place) if a_name.hasAttribute('isPreferredName') and (pattr and pattr == self.lang): # if not preferred lang, we use topo name, otherwise # preferred name for lang self.newplace.name = new_place # Try to deduce PlaceType: # If populated place, set as City. Long description could over-ride # Parse long description, looking for keyword (Region, County, ...) # Top-level must be a country. # Children of USA are States. # Children of Canada are Provinces. # value = g_name.getElementsByTagName('fcl') fcl = value[0].childNodes[0].data value = g_name.getElementsByTagName('fcode') fcode = value[0].childNodes[0].data value = g_name.getElementsByTagName('countryCode') countrycode = value[0].childNodes[0].data self.newplace.place_type = PlaceType(PlaceType.UNKNOWN) ptype = PlaceType() # scan thorough names looking for name portion that matches a Placetype for name in self.newplace.names: for tname in name.value.split(' '): ptype.set_from_xml_str(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is a translated PlaceType ptype.set(tname.capitalize()) if ptype != PlaceType.CUSTOM: self.newplace.place_type = ptype break # see if it is an already added custom type cust_types = self.dbstate.db.get_place_types() if tname.capitalize() in cust_types: self.newplace.place_type = ptype break else: # Continue if the inner loop wasn't broken. continue # Inner loop was broken, break the outer. break if fcl == 'P': self.newplace.place_type = PlaceType(PlaceType.CITY) elif fcode == 'PRSH': self.newplace.place_type = PlaceType(PlaceType.PARISH) elif 'PCL' in fcode: self.newplace.place_type = PlaceType(PlaceType.COUNTRY) elif 'ADM' in fcode: if countrycode in self.adm_table: _ptype = self.adm_table[countrycode].get(fcode[:4]) if _ptype and (_ptype[1] or self.newplace.place_type.is_default()): self.newplace.place_type = PlaceType(_ptype[0]) # save a parent for enclosing if len(h_geoid_list) > 1: # we have a parent self.newplace.parent_names = h_name_list[1:] self.newplace.parent_ids = h_geoid_list[1:] return True
def _process(self, count, total, sql): # --------------------------------- # Process note # --------------------------------- notes = sql.query("""select * from note;""") for note in notes: (handle, gid, text, _format, note_type1, note_type2, change, private) = note styled_text = [text, []] markups = sql.query( """select * from link where from_handle = ? """ """and to_type = 'markup';""", handle) for markup_link in markups: _from_type, _from_handle, _to_type, to_handle = markup_link markup_detail = sql.query( """select * from markup where handle = ?;""", to_handle) for markup in markup_detail: (_mhandle, markup0, markup1, value, start_stop_list) = markup ss_list = eval(start_stop_list) styled_text[1] += [((markup0, markup1), value, ss_list)] tags = self.get_links(sql, "note", handle, "tag") g_note = Note() g_note.unserialize( (handle, gid, styled_text, _format, (note_type1, note_type2), change, tags, bool(private))) self.db.add_note(g_note, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process event # --------------------------------- events = sql.query("""select * from event;""") for event in events: (handle, gid, the_type0, the_type1, description, change, private) = event note_list = self.get_note_list(sql, "event", handle) citation_list = self.get_citation_list(sql, "event", handle) media_list = self.get_media_list(sql, "event", handle) attribute_list = self.get_attribute_list(sql, "event", handle) date_handle = self.get_link(sql, "event", handle, "date") date = self.get_date(sql, date_handle) place_handle = self.get_link(sql, "event", handle, "place") place = self.get_place_from_handle(sql, place_handle) tags = self.get_links(sql, "event", handle, "tag") data = (handle, gid, (the_type0, the_type1), date, description, place, citation_list, note_list, media_list, attribute_list, change, tags, bool(private)) g_event = Event() g_event.unserialize(data) self.db.add_event(g_event, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process person # --------------------------------- people = sql.query("""select * from person;""") for person in people: if person is None: continue ( handle, # 0 gid, # 1 gender, # 2 death_ref_handle, # 5 birth_ref_handle, # 6 change, # 17 private, # 19 ) = person primary_name = self.get_names(sql, "person", handle, True) # one alternate_names = self.get_names(sql, "person", handle, False) event_ref_list = self.get_event_ref_list(sql, "person", handle) family_list = self.get_family_list(sql, "person", handle) parent_family_list = self.get_parent_family_list( sql, "person", handle) media_list = self.get_media_list(sql, "person", handle) address_list = self.get_address_list(sql, "person", handle, with_parish=False) attribute_list = self.get_attribute_list(sql, "person", handle) urls = self.get_url_list(sql, "person", handle) lds_ord_list = self.get_lds_list(sql, "person", handle) pcitation_list = self.get_citation_list(sql, "person", handle) pnote_list = self.get_note_list(sql, "person", handle) person_ref_list = self.get_person_ref_list(sql, "person", handle) death_ref_index = lookup(death_ref_handle, event_ref_list) birth_ref_index = lookup(birth_ref_handle, event_ref_list) tags = self.get_links(sql, "person", handle, "tag") data = ( handle, # 0 gid, # 1 gender, # 2 primary_name, # 3 alternate_names, # 4 death_ref_index, # 5 birth_ref_index, # 6 event_ref_list, # 7 family_list, # 8 parent_family_list, # 9 media_list, # 10 address_list, # 11 attribute_list, # 12 urls, # 13 lds_ord_list, # 14 pcitation_list, # 15 pnote_list, # 16 change, # 17 tags, bool(private), # 19 person_ref_list, ) # 20 g_pers = Person() g_pers.unserialize(data) self.db.add_person(g_pers, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process family # --------------------------------- families = sql.query("""select * from family;""") for family in families: (handle, gid, father_handle, mother_handle, the_type0, the_type1, change, private) = family child_ref_list = self.get_child_ref_list(sql, "family", handle) event_ref_list = self.get_event_ref_list(sql, "family", handle) media_list = self.get_media_list(sql, "family", handle) attribute_list = self.get_attribute_list(sql, "family", handle) lds_seal_list = self.get_lds_list(sql, "family", handle) citation_list = self.get_citation_list(sql, "family", handle) note_list = self.get_note_list(sql, "family", handle) tags = self.get_links(sql, "family", handle, "tag") data = (handle, gid, father_handle, mother_handle, child_ref_list, (the_type0, the_type1), event_ref_list, media_list, attribute_list, lds_seal_list, citation_list, note_list, change, tags, private) g_fam = Family() g_fam.unserialize(data) self.db.add_family(g_fam, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process repository # --------------------------------- repositories = sql.query("""select * from repository;""") for repo in repositories: (handle, gid, the_type0, the_type1, name, change, private) = repo note_list = self.get_note_list(sql, "repository", handle) address_list = self.get_address_list(sql, "repository", handle, with_parish=False) urls = self.get_url_list(sql, "repository", handle) tags = self.get_links(sql, "repository", handle, "tag") data = (handle, gid, (the_type0, the_type1), name, note_list, address_list, urls, change, tags, private) g_rep = Repository() g_rep.unserialize(data) self.db.add_repository(g_rep, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process place # --------------------------------- places = sql.query("""select * from place;""") for place in places: count += 1 (handle, gid, title, value, the_type0, the_type1, code, long, lat, lang, change, private) = place # We could look this up by "place_main", but we have the handle: #main_loc = self.get_main_location(sql, handle, with_parish=True) alt_loc_list = self.get_location_list(sql, "place_alt", handle, with_parish=True) urls = self.get_url_list(sql, "place", handle) media_list = self.get_media_list(sql, "place", handle) citation_list = self.get_citation_list(sql, "place", handle) note_list = self.get_note_list(sql, "place", handle) tags = self.get_links(sql, "place", handle, "tag") place_type = (the_type0, the_type1) alt_place_name_list = self.get_alt_place_name_list(sql, handle) place_ref_list = self.get_place_ref_list(sql, handle) data = (handle, gid, title, long, lat, place_ref_list, PlaceName(value=value, lang=lang).serialize(), alt_place_name_list, place_type, code, alt_loc_list, urls, media_list, citation_list, note_list, change, tags, private) g_plac = Place() g_plac.unserialize(data) self.db.commit_place(g_plac, self.trans) self.callback(100 * count / total) # --------------------------------- # Process citation # --------------------------------- citations = sql.query("""select * from citation;""") for citation in citations: (handle, gid, confidence, page, source_handle, change, private) = citation date_handle = self.get_link(sql, "citation", handle, "date") date = self.get_date(sql, date_handle) note_list = self.get_note_list(sql, "citation", handle) media_list = self.get_media_list(sql, "citation", handle) datamap = self.get_datamap_list(sql, "citation", handle) tags = self.get_links(sql, "citation", handle, "tag") data = (handle, gid, date, page, confidence, source_handle, note_list, media_list, datamap, change, tags, private) g_cit = Citation() g_cit.unserialize(data) self.db.commit_citation(g_cit, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process source # --------------------------------- sources = sql.query("""select * from source;""") for source in sources: (handle, gid, title, author, pubinfo, abbrev, change, private) = source note_list = self.get_note_list(sql, "source", handle) media_list = self.get_media_list(sql, "source", handle) datamap = self.get_datamap_list(sql, "source", handle) reporef_list = self.get_repository_ref_list(sql, "source", handle) tags = self.get_links(sql, "source", handle, "tag") data = (handle, gid, title, author, pubinfo, note_list, media_list, abbrev, change, datamap, reporef_list, tags, private) g_src = Source() g_src.unserialize(data) self.db.commit_source(g_src, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process media # --------------------------------- media = sql.query("""select * from media;""") for med in media: (handle, gid, path, mime, desc, checksum, change, private) = med attribute_list = self.get_attribute_list(sql, "media", handle) citation_list = self.get_citation_list(sql, "media", handle) note_list = self.get_note_list(sql, "media", handle) date_handle = self.get_link(sql, "media", handle, "date") date = self.get_date(sql, date_handle) tags = self.get_links(sql, "media", handle, "tag") data = (handle, gid, path, mime, desc, checksum, attribute_list, citation_list, note_list, change, date, tags, private) g_med = Media() g_med.unserialize(data) self.db.commit_media(g_med, self.trans) count += 1 self.callback(100 * count / total) # --------------------------------- # Process tag # --------------------------------- tags = sql.query("""select * from tag;""") for tag in tags: (handle, name, color, priority, change) = tag data = (handle, name, color, priority, change) g_tag = Tag() g_tag.unserialize(data) self.db.commit_tag(g_tag, self.trans) count += 1 self.callback(100 * count / total)