Пример #1
0
 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
Пример #2
0
    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