def find_or_make_source(self): """ Find or create a source. returns handle to source.""" for hndl in self.dbstate.db.get_source_handles(): if self.dbstate.db.get_raw_source_data(hndl)[2] == 'GeoNames': return hndl # No source found, lets add one with associated repo and note repo = Repository() repo.set_name("www.geonames.org") rtype = RepositoryType(RepositoryType.WEBSITE) repo.set_type(rtype) url = Url() url.set_path('http://www.geonames.org/') url.set_description(_('GeoNames web site')) url.set_type(UrlType(UrlType.WEB_HOME)) repo.add_url(url) url = Url() url.set_path('*****@*****.**') url.set_description(_('GeoNames author')) url.set_type(UrlType(UrlType.EMAIL)) repo.add_url(url) note_txt = StyledText( _('GeoNames was founded by Marc Wick. You can reach him at ')) note_txt += StyledText('*****@*****.**' + '\n') note_txt += StyledText( _('GeoNames is a project of Unxos GmbH, Weingartenstrasse 8,' ' 8708 Männedorf, Switzerland.\nThis work is licensed under a ')) note_txt += linkst( _('Creative Commons Attribution 3.0 License'), 'https://creativecommons.org/licenses/by/3.0/legalcode') new_note = Note() new_note.set_styledtext(note_txt) new_note.set_type(NoteType.REPO) src = Source() src.title = 'GeoNames' src.author = 'Marc Wick' repo_ref = RepoRef() mtype = SourceMediaType(SourceMediaType.ELECTRONIC) repo_ref.set_media_type(mtype) with DbTxn( _("Add Souce/Repo/Note (%s)") % "GeoNames", self.dbstate.db) as trans: self.dbstate.db.add_note(new_note, trans) repo.add_note(new_note.get_handle()) self.dbstate.db.add_repository(repo, trans) repo_ref.set_reference_handle(repo.handle) src.add_repo_reference(repo_ref) self.dbstate.db.add_source(src, trans) return src.handle
def add_url(self, fields, data): """Read the URL property of a VCard.""" href = data.strip() if href: url = Url() url.set_path(self.unesc(href)) self.person.add_url(url)
def add_button_clicked(self, obj): from .. import EditUrl url = Url() try: EditUrl(self.dbstate, self.uistate, self.track, '', url, self.add_callback) except WindowActiveError: pass
def column_home_url(self, data): if data[6]: for i in data[6]: url = Url() url.unserialize(i) if url.get_type() == UrlType.WEB_HOME: return url.path return ""
def column_search_url(self, data): if data[6]: for i in data[6]: url = Url() url.unserialize(i) if url.get_type() == UrlType.WEB_SEARCH: return url.path return ''
def column_email(self, data): if data[6]: for i in data[6]: url = Url() url.unserialize(i) if url.get_type() == UrlType.EMAIL: return url.path return ''
def add_email(self, fields, data): """Read the EMAIL property of a VCard.""" email = data.strip() if email: url = Url() url.set_type(UrlType(UrlType.EMAIL)) url.set_path(self.unesc(email)) self.person.add_url(url)
def __get_hasurl(self, element): url = Url() pobj = element.getElementsByTagName('gov:PropertyForObject') if len(pobj): value = pobj[0].getElementsByTagName('gov:value') if len(value): url.set_path(value[0].childNodes[0].data) url.set_type(UrlType.WEB_HOME) return url
def addRepository(repositoryName, repositoryUrl, reftag): ridno = db.find_next_repository_gramps_id() repository = Repository() repositoryType = RepositoryType() repositoryType.set(RepositoryType.ARCHIVE) repository.set_type(repositoryType) repository.set_gramps_id(ridno) repository.set_name(repositoryName) urlList = [] # for urlPath in repositoryUrls: url = Url() url.set_path(repositoryUrl) url.set_description('Sshy') url.set_type(UrlType.WEB_HOME) urlList.append(url) repository.set_url_list(urlList) repository.set_change_time(chgtime) if reftag != None: repository.add_tag(reftag.get_handle()) with DbTxn(_("Add Repository"), db) as trans: rhandle = db.add_repository(repository, trans) return repository
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 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