def add_stop(self, meta, entity_type, source): try: entity = Entity.objects.get(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']) except Entity.DoesNotExist: entity = Entity(source=source) cnm, lmk, ind, str = [meta.get(k) for k in ['common-name', 'landmark', 'indicator', 'street']] if (cnm or '').endswith(' DEL') or (ind or '').lower == 'not in use': return if lmk and ind and ind.endswith(lmk) and len(ind) > len(lmk): ind = ind[:-len(lmk)] ind = { 'opp': 'Opposite', 'opposite': 'Opposite', 'adj': 'Adjacent to', 'outside': 'Outside', 'o/s': 'Outside', 'nr': 'Near', 'inside': 'Inside', }.get(ind, ind) if meta['stop-type'] == 'RSE': title = cnm elif (ind or '').lower() == 'corner': title = "Corner of %s and %s" % (str, lmk) elif cnm == str: if ind in ('Opposite','Adjacent to','Outside','Near'): title = "%s %s" % (ind, cnm) else: title = "%s, %s" % (ind, cnm) elif ind == lmk: title = "%s, %s" % (lmk, str) elif lmk != str: title = "%s %s, on %s" % (ind, lmk, str) else: title = "%s %s, %s" % (ind, lmk, cnm) entity.title = title entity.primary_type = entity_type if not entity.metadata: entity.metadata = {} entity.metadata['naptan'] = meta entity.location = Point(float(meta['longitude']), float(meta['latitude']), srid=4326) entity.geometry = entity.location identifiers = { 'atco': meta['atco-code'], } if 'naptan-code' in meta: meta['naptan-code'] = ''.join(map(self.naptan_dial, meta['naptan-code'])) identifiers['naptan'] = meta['naptan-code'] if ind and re.match('Stop [A-Z]\d\d?', ind): identifiers['stop'] = ind[5:] entity.save(identifiers=identifiers) entity.all_types.add(entity_type) entity.update_all_types_completion() return entity
def add_stop(self, meta, entity_type, source, is_entrance): # Check this entity is in an area if self.areas != None: in_area = False for area in self.areas: if meta['atco-code'].startswith(area): in_area = True if not in_area: return # See if we're updating an existing object, or creating a new one try: entity = Entity.objects.get(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']) except Entity.DoesNotExist: entity = Entity(source=source) except Entity.MultipleObjectsReturned: # Handle clashes Entity.objects.filter(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']).delete() entity = Entity(source=source) common_name, indicator, locality, street = [meta.get(k) for k in ('common-name', 'indicator', 'locality-ref', 'street')] if (common_name or '').endswith(' DEL') or \ (indicator or '').lower() == 'not in use' or \ 'to define route' in (common_name or '') or \ 'to def rte' in (common_name or '') or \ 'to def route' in (common_name or '') or \ 'def.rte' in (common_name or ''): # In the NaPTAN list, but indicates it's an unused stop return if self.meta['stop-type'] in ('MET','GAT','FER', 'RLY'): names = self.names else: names = dict() for lang_code, lang_name in settings.LANGUAGES: with override(lang_code): # Try and find one in our preferred order for lang in (lang_code, 'en', None): if lang in self.names: common_name = self.names[lang] break # Expand abbreviations in indicators if indicator is not None: parts = [] for part in indicator.split(): parts.append({ # Translators: This is referring to bus stop location descriptions 'op': ugettext('Opposite'), 'opp': ugettext('Opposite'), 'opposite': ugettext('Opposite'), # Translators: This is referring to bus stop location descriptions 'adj': ugettext('Adjacent'), # Translators: This is referring to bus stop location descriptions 'outside': ugettext('Outside'), 'o/s': ugettext('Outside'), # Translators: This is referring to bus stop location descriptions 'nr': ugettext('Near'), # Translators: This is referring to bus stop location descriptions 'inside': ugettext('Inside'), # Translators: This is referring to bus stop location descriptions 'stp': ugettext('Stop'), }.get(part.lower(), part)) indicator = ' '.join(parts) if indicator is None and self.meta['stop-type'] in ('AIR', 'FTD', 'RSE', 'TMU', 'BCE'): # Translators: This is referring to public transport entities title = ugettext('Entrance to %s') % common_name elif indicator is None and self.meta['stop-type'] in ('FBT',): # Translators: This is referring to ferry ports title = ugettext('Berth at %s') % common_name elif indicator is None and self.meta['stop-type'] in ('RPL','PLT'): # Translators: This is referring to rail and metro stations title = ugettext('Platform at %s') % common_name elif indicator is not None and indicator.lower() != 'none' \ and indicator not in common_name: title = indicator + ' ' + common_name else: title = common_name if street not in (None, '-', '---'): # Deal with all-caps street names if street.upper() == street: fixedstreet = '' wordstart = True for letter in street: if wordstart: wordstart = False fixedstreet += letter continue elif letter == ' ': wordstart = True fixedstreet += letter continue else: fixedstreet += letter.lower() street = fixedstreet if street not in title: title += ', ' + street locality_lang = self.nptg_localities.get(locality) if locality_lang != None: for lang in (lang_code, 'en', 'cy'): if lang in locality_lang: if locality_lang[lang] != street: title += ', ' + locality_lang[lang] break names[lang_code] = title entity.primary_type = entity_type entity.is_entrance = is_entrance if not entity.metadata: entity.metadata = {} entity.metadata['naptan'] = meta entity.location = Point(float(meta['longitude']), float(meta['latitude']), srid=4326) entity.geometry = entity.location if meta['atco-code'] in self.tube_references: entity.metadata['london-underground-identifiers'] = self.tube_references[meta['atco-code']] identifiers = { 'atco': meta['atco-code'], } if 'naptan-code' in meta: meta['naptan-code'] = ''.join(map(self.naptan_dial, meta['naptan-code'])) identifiers['naptan'] = meta['naptan-code'] if 'plate-code' in meta: identifiers['plate'] = meta['plate-code'] if 'crs' in meta: identifiers['crs'] = meta['crs'] if 'tiploc' in meta: identifiers['tiploc'] = meta['tiploc'] if indicator != None and re.match('Stop [A-Z]\d\d?', indicator): identifiers['stop'] = indicator[5:] entity.save(identifiers=identifiers) for lang_code, name in names.items(): # This is the NaPTAN, so default to English if lang_code is None: lang_code = 'en' set_name_in_language(entity, lang_code, title=name) entity.all_types = (entity_type,) entity.update_all_types_completion() entity.groups.clear() for stop_area in self.stop_areas: sa, created = EntityGroup.objects.get_or_create(source=source, ref_code=stop_area) entity.groups.add(sa) entity.save() return entity
def add_stop(self, meta, entity_type, source): # Check this entity is in an area if self.areas != None: in_area = False for area in self.areas: if meta['atco-code'].startswith(area): in_area = True if not in_area: return # See if we're updating an existing object, or creating a new one try: entity = Entity.objects.get(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']) except Entity.DoesNotExist: entity = Entity(source=source) except Entity.MultipleObjectsReturned: # Handle clashes Entity.objects.filter(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']).delete() entity = Entity(source=source) common_name, indicator, locality, street = [meta.get(k) for k in ('common-name', 'indicator', 'locality-ref', 'street')] if (common_name or '').endswith(' DEL') or \ (indicator or '').lower() == 'not in use': # In the NaPTAN list, but indicates it's an unused stop return # Convert indicator to a friendlier format indicator = { 'opp': 'Opposite', 'opposite': 'Opposite', 'adj': 'Adjacent', 'outside': 'Outside', 'o/s': 'Outside', 'nr': 'Near', 'inside': 'Inside', }.get(indicator, indicator) title = '' if indicator != None: title += indicator + ' ' title += common_name if street != None and not common_name.startswith(street): title += ', ' + street locality = self.nptg_localities.get(locality) if locality != None: title += ', ' + locality entity.title = title entity.primary_type = entity_type if not entity.metadata: entity.metadata = {} entity.metadata['naptan'] = meta entity.location = Point(float(meta['longitude']), float(meta['latitude']), srid=4326) entity.geometry = entity.location identifiers = { 'atco': meta['atco-code'], } if 'naptan-code' in meta: meta['naptan-code'] = ''.join(map(self.naptan_dial, meta['naptan-code'])) identifiers['naptan'] = meta['naptan-code'] if 'plate-code' in meta: identifiers['plate'] = meta['plate-code'] if indicator != None and re.match('Stop [A-Z]\d\d?', indicator): identifiers['stop'] = indicator[5:] entity.save(identifiers=identifiers) entity.all_types.add(entity_type) entity.update_all_types_completion() return entity
def add_stop(self, meta, entity_type, source): # Check this entity is in an area if self.areas != None: in_area = False for area in self.areas: if meta['atco-code'].startswith(area): in_area = True if not in_area: return # See if we're updating an existing object, or creating a new one try: entity = Entity.objects.get(source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']) except Entity.DoesNotExist: entity = Entity(source=source) except Entity.MultipleObjectsReturned: # Handle clashes Entity.objects.filter( source=source, _identifiers__scheme='atco', _identifiers__value=meta['atco-code']).delete() entity = Entity(source=source) common_name, indicator, locality, street = [ meta.get(k) for k in ('common-name', 'indicator', 'locality-ref', 'street') ] if (common_name or '').endswith(' DEL') or \ (indicator or '').lower() == 'not in use' or \ 'to define route' in (common_name or '') or \ 'to def rte' in (common_name or '') or \ 'to def route' in (common_name or '') or \ 'def.rte' in (common_name or ''): # In the NaPTAN list, but indicates it's an unused stop return if self.meta['stop-type'] in ('MET', 'GAT', 'FER', 'RLY'): names = self.names else: names = dict() for lang_code, lang_name in settings.LANGUAGES: with override(lang_code): # Try and find one in our preferred order for lang in (lang_code, 'en', None): if lang in self.names: common_name = self.names[lang] break # Expand abbreviations in indicators if indicator is not None: parts = [] for part in indicator.split(): parts.append({ # Translators: This is referring to bus stop location descriptions 'op': ugettext('Opposite'), 'opp': ugettext('Opposite'), 'opposite': ugettext('Opposite'), # Translators: This is referring to bus stop location descriptions 'adj': ugettext('Adjacent'), # Translators: This is referring to bus stop location descriptions 'outside': ugettext('Outside'), 'o/s': ugettext('Outside'), # Translators: This is referring to bus stop location descriptions 'nr': ugettext('Near'), # Translators: This is referring to bus stop location descriptions 'inside': ugettext('Inside'), # Translators: This is referring to bus stop location descriptions 'stp': ugettext('Stop'), }.get(part.lower(), part)) indicator = ' '.join(parts) if indicator is None and self.meta['stop-type'] in ( 'AIR', 'FTD', 'RSE', 'TMU', 'BCE'): # Translators: This is referring to public transport entities title = ugettext('Entrance to %s') % common_name elif indicator is None and self.meta['stop-type'] in ( 'FBT', ): # Translators: This is referring to ferry ports title = ugettext('Berth at %s') % common_name elif indicator is None and self.meta['stop-type'] in ( 'RPL', 'PLT'): # Translators: This is referring to rail and metro stations title = ugettext('Platform at %s') % common_name elif indicator is not None and indicator.lower() != 'none' \ and indicator not in common_name: title = indicator + ' ' + common_name else: title = common_name if street != None and street != '-': # Deal with all-caps street names if street.upper() == street: fixedstreet = '' wordstart = True for letter in street: if wordstart: wordstart = False fixedstreet += letter continue elif letter == ' ': wordstart = True fixedstreet += letter continue else: fixedstreet += letter.lower() street = fixedstreet if street not in title: title += ', ' + street locality_lang = self.nptg_localities.get(locality) if locality_lang != None: for lang in (lang_code, 'en', 'cy'): if lang in locality_lang: if locality_lang[lang] != street: title += ', ' + locality_lang[lang] break names[lang_code] = title entity.primary_type = entity_type if not entity.metadata: entity.metadata = {} entity.metadata['naptan'] = meta entity.location = Point(float(meta['longitude']), float(meta['latitude']), srid=4326) entity.geometry = entity.location if meta['atco-code'] in self.tube_references: entity.metadata[ 'london-underground-identifiers'] = self.tube_references[ meta['atco-code']] identifiers = { 'atco': meta['atco-code'], } if 'naptan-code' in meta: meta['naptan-code'] = ''.join( map(self.naptan_dial, meta['naptan-code'])) identifiers['naptan'] = meta['naptan-code'] if 'plate-code' in meta: identifiers['plate'] = meta['plate-code'] if 'crs' in meta: identifiers['crs'] = meta['crs'] if indicator != None and re.match('Stop [A-Z]\d\d?', indicator): identifiers['stop'] = indicator[5:] entity.save(identifiers=identifiers) for lang_code, name in names.items(): # This is the NaPTAN, so default to English if lang_code is None: lang_code = 'en' set_name_in_language(entity, lang_code, title=name) entity.all_types = (entity_type, ) entity.update_all_types_completion() entity.groups.clear() for stop_area in self.stop_areas: sa, created = EntityGroup.objects.get_or_create(source=source, ref_code=stop_area) entity.groups.add(sa) entity.save() return entity