Exemplo n.º 1
0
    def _get_contained_items_from_db(self, page, per_page):
        cic = self.contained_item_classes[0]
        try:
            cursor = config.get_db_cursor()
            if cic == 'http://linked.data.gov.au/def/gnaf#Address':
                id_query = sql.SQL('''SELECT address_detail_pid
                                   FROM gnaf.address_detail
                                   ORDER BY address_detail_pid
                                   LIMIT {limit}
                                   OFFSET {offset}''').format(
                    limit=sql.Literal(per_page),
                    offset=sql.Literal((page - 1) * per_page))
                label_prefix = 'Address'
            elif cic == 'http://linked.data.gov.au/def/gnaf#Locality':
                id_query = sql.SQL('''SELECT locality_pid
                                   FROM gnaf.locality
                                   ORDER BY locality_pid
                                   LIMIT {limit}
                                   OFFSET {offset}''').format(
                    limit=sql.Literal(per_page),
                    offset=sql.Literal((page - 1) * per_page))
                label_prefix = 'Locality'
            elif cic == 'http://linked.data.gov.au/def/gnaf#StreetLocality':
                id_query = sql.SQL('''SELECT street_locality_pid
                                   FROM gnaf.street_locality
                                   ORDER BY street_locality_pid
                                   LIMIT {limit}
                                   OFFSET {offset}''').format(
                    limit=sql.Literal(per_page),
                    offset=sql.Literal((page - 1) * per_page))
                label_prefix = 'Street Locality'
            elif cic == 'http://linked.data.gov.au/def/gnaf#AddressSite':
                id_query = sql.SQL('''SELECT address_site_pid
                                   FROM gnaf.address_site
                                   ORDER BY address_site_pid
                                   LIMIT {limit}
                                   OFFSET {offset}''').format(
                    limit=sql.Literal(per_page),
                    offset=sql.Literal((page - 1) * per_page))
                label_prefix = 'Address Site'
            else:
                raise RuntimeError(
                    "Cannot get DB objects for unknown contained item class.")

            cursor.execute(id_query)
            rows = cursor.fetchall()
            for row in rows:
                item_pid = row[0]
                uri = ''.join([self.uri, item_pid])
                label = ' '.join([label_prefix, 'ID:', item_pid])
                self.register_items.append((uri, label, item_pid))
        except Exception as e:
            print(
                "Uh oh, can't connect to DB. Invalid dbname, user or password?"
            )
            print(e)
Exemplo n.º 2
0
    def export_rdf(self, view='ISO19160', format='text/turtle'):
        g = Graph()
        a = URIRef(self.uri)

        if view == 'ISO19160':
            # get the components from the DB needed for ISO19160
            s = sql.SQL('''SELECT 
                        address_type, 
                        address_site_name                       
                    FROM {dbschema}.address_site
                    WHERE address_site_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            try:
                cursor = config.get_db_cursor()
                # get just IDs, ordered, from the address_detail table, paginated by class init args
                cursor.execute(s)
                for record in cursor:
                    address_type = record[0]
                    address_site_name = record[1]
            except Exception as e:
                print(
                    "Uh oh, can't connect to DB. Invalid dbname, user or password?"
                )
                print(e)

            AddressComponentTypeUriBase = 'http://def.isotc211.org/iso19160/-1/2015/Address/code/AddressComponentType/'

            ISO = Namespace(
                'http://def.isotc211.org/iso19160/-1/2015/Address#')
            g.bind('iso19160', ISO)

            g.add((a, RDF.type, ISO.Address))

            ac_site_name = BNode()
            g.add((ac_site_name, RDF.type, ISO.AddressComponent))
            g.add((ac_site_name, ISO.valueInformation,
                   Literal(address_site_name, datatype=XSD.string)))
            g.add((ac_site_name, ISO.type,
                   URIRef(AddressComponentTypeUriBase + 'siteName')))
            g.add((a, ISO.addressComponent, ac_site_name))

        elif view == 'gnaf':
            raise NotImplementedError(
                "RDF Representation of the GNAF View of an addressSite is not yet implemented."
            )
            # TODO: implement DCT RDF
        elif view == 'dct':
            raise NotImplementedError(
                "RDF Representation of the DCT View of an addressSite is not yet implemented."
            )
            # TODO: implement DCT RDF
        else:
            raise RuntimeError(
                "Cannot render an RDF representation of that View.")
        return g
Exemplo n.º 3
0
def get_localities_in_state(state_pid):
    cursor = config.get_db_cursor()
    s = '''SELECT locality_pid 
           FROM gnaf.locality 
           WHERE state_pid = '{}'
           ORDER BY locality_pid;'''.format(state_pid)
    cursor.execute(s)

    with open('localities_state_{}.txt'.format(state_pid), 'w') as f:
        for row in cursor.fetchall():
            r = config.reg(cursor, row)
            f.write(str(r.locality_pid) + '\n')
Exemplo n.º 4
0
def get_state_addresses(state_pid):
    cursor = config.get_db_cursor()
    s = '''SELECT address_detail_pid 
           FROM gnaf.address_detail 
           WHERE locality_pid 
           IN (SELECT locality_pid FROM gnaf.locality WHERE state_pid = '{}') 
           ORDER BY address_detail_pid;'''.format(state_pid)
    cursor.execute(s)

    with open('addresses_state_{}.txt'.format(state_pid), 'w') as f:
        for row in cursor.fetchall():
            r = config.reg(cursor, row)
            f.write(str(r.address_detail_pid) + '\n')
Exemplo n.º 5
0
def run(index_file, file_id, data_file_count):
    logging.basicConfig(filename='graph_builder.log',
                        level=logging.DEBUG,
                        datefmt='%Y-%m-%d %H:%M:%S',
                        format='%(asctime)s %(message)s')
    '''
    - For each Address in the Address Register,
    - Create an Address class instance using the ID
    - Serialise that to a file
    '''
    DATA_FILE_LENGTH_MAX = 100000

    cursor = config.get_db_cursor()

    data_file_stem = 'data-address-' + str(file_id) + "-"
    # for idx, addr in enumerate(cursor.fetchall()):
    for idx, addr in enumerate(open(index_file, 'r').readlines()):
        a = addr.strip()
        try:
            # record the Address being processed in case of failure
            # every DATA_FILE_LENGTH_MAXth URI, create a new destination file
            if (idx + 1) % DATA_FILE_LENGTH_MAX == 0:
                data_file_count += 1
            with open(data_file_stem + str(data_file_count).zfill(4) + '.nt',
                      'a') as fl:
                fl.write(
                    model.address.Address(
                        a, focus=True,
                        db_cursor=cursor).export_rdf(view='gnaf').serialize(
                            format='nt').decode('utf-8'))
        except Exception as e:
            logging.log(logging.DEBUG, 'address ' + a, e)
            print('address ' + a + '\n')
            print(e)
            with open('faulty.log', 'a') as f:
                f.write(addr + '\n')
        finally:
            logging.log(logging.INFO, 'Last accessed Address: ' + a)
Exemplo n.º 6
0
    def export_html(self, view='gnaf'):
        # connect to DB
        cursor = config.get_db_cursor()

        if view == 'gnaf':
            # make a human-readable address
            s = sql.SQL('''SELECT 
                        address_type, 
                        address_site_name                       
                    FROM {dbschema}.address_site
                    WHERE address_site_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s)
            rows = cursor.fetchall()
            for row in rows:
                address_type = row[0]
                address_site_name = row[1]

            # get a list of addressSiteGeocodeIds from the address_site_geocode table
            s2 = sql.SQL('''SELECT 
                        address_site_geocode_pid,
                        geocode_type                
                    FROM {dbschema}.address_site_geocode_view
                    WHERE address_site_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s2)
            rows = cursor.fetchall()
            for row in rows:
                self.address_site_geocode_ids[row[0]] = row[1].title()

            view_html = render_template(
                'class_addressSite_gnaf.html',
                address_site_pid=self.id,
                address_site_name=address_site_name,
                address_type=address_type,
                address_site_geocode_ids=self.address_site_geocode_ids)

        elif view == 'ISO19160':
            view_html = render_template('class_addressSite_ISO19160.html', )

        elif view == 'dct':
            s = sql.SQL('''SELECT 
                        address_type, 
                        address_site_name                       
                    FROM {dbschema}.address_site
                    WHERE address_site_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s)
            for record in cursor:
                address_type = record[0]
                address_site_name = record[1]

            view_html = render_template('class_addressSite_dct.html',
                                        identifier=self.id,
                                        title='Address Site' + self.id,
                                        description=address_site_name,
                                        source='G-NAF, 2016',
                                        type='AddressSite')
        else:
            return NotImplementedError(
                "HTML representation of View '{}' is not implemented.".format(
                    view))
        return view_html
Exemplo n.º 7
0
    def export_html(self, view='gnaf'):
        if view == 'gnaf':
            # initialise parameters in case no results are returned from SQL
            latitude = None
            longitude = None
            geocode_type = None
            locality_name = None
            geometry_wkt = None
            street_string = None

            # make a human-readable street
            s = sql.SQL('''SELECT 
                        a.street_name, 
                        a.street_type_code, 
                        a.street_suffix_code,
                        a.latitude,
                        a.longitude,
                        a.geocode_type,
                        a.locality_pid,
                        b.locality_name,
                        st.prefLabel AS street_type_label,
                        st.uri AS street_type_uri,
                        ss.prefLabel AS street_suffix_label,
                        ss.uri AS street_suffix_uri
                    FROM {dbschema}.street_view a
                      INNER JOIN {dbschema}.locality_view b ON a.locality_pid = b.locality_pid
                      LEFT JOIN codes.street st ON a.street_type_code = st.code
                      LEFT JOIN codes.streetsuffix ss ON a.street_suffix_code = ss.code
                    WHERE street_locality_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            cursor = config.get_db_cursor()

            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s)
            rows = cursor.fetchall()
            for row in rows:
                r = config.reg(cursor, row)
                self.street_name = r.street_name.title()
                self.street_type = r.street_type_code.title()
                self.street_suffix = r.street_suffix_code.title(
                ) if r.street_suffix_code is not None else None
                latitude = r.latitude
                longitude = r.longitude
                geocode_type = r.geocode_type.title(
                ) if r.geocode_type is not None else None
                self.locality_pid = r.locality_pid
                locality_name = r.locality_name.title()
                self.street_type_label = r.street_type_label
                self.street_type_uri = r.street_type_uri
                self.street_suffix_label = r.street_suffix_label
                self.street_suffix_uri = r.street_suffix_uri
                geometry_wkt = self.make_wkt_literal(
                    longitude=longitude,
                    latitude=latitude) if latitude is not None else None

                street_string = '{}{}{}'.format(
                    self.street_name, ' ' + self.street_type.title()
                    if self.street_type is not None else '',
                    ' ' + self.street_suffix.title()
                    if self.street_suffix is not None else '')

            # aliases
            s2 = sql.SQL('''SELECT 
                          street_locality_alias_pid,
                          street_name,
                          street_type_code,
                          street_suffix_code,
                          alias_type_code
                        FROM {dbschema}.street_locality_alias 
                        WHERE street_locality_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            cursor.execute(s2)
            rows = cursor.fetchall()
            for row in rows:
                r = config.reg(cursor, row)
                street_string = '{}{}{}'.format(
                    r.street_name, ' ' + r.street_type_code.title()
                    if r.street_type_code is not None else '',
                    ' ' + r.street_suffix_code.title()
                    if r.street_suffix_code is not None else '')
                self.street_locality_aliases[
                    r.street_locality_alias_pid] = street_string

            view_html = render_template(
                'class_streetLocality_gnaf.html',
                street_string=street_string,
                street_locality_pid=self.id,
                street_name=self.street_name,
                street_type=self.street_type,
                street_type_label=self.street_type_label,
                street_type_uri=self.street_type_uri,
                street_suffix=self.street_suffix,
                street_suffix_label=self.street_suffix_label,
                street_suffix_uri=self.street_suffix_uri,
                locality_name=locality_name,
                latitude=latitude,
                longitude=longitude,
                geocode_uri=
                'http://linked.data.gov.au/def/gnaf/code/GeocodeTypes#StreetLocality',
                geocode_label=geocode_type,
                geometry_wkt=geometry_wkt,
                locality_pid=self.locality_pid,
                street_locality_aliases=self.street_locality_aliases)

        elif view == 'ISO19160':
            view_html = render_template('class_streetLocality_ISO19160.html', )

        elif view == 'dct':
            s = sql.SQL('''SELECT 
                        street_name, 
                        street_type_code, 
                        street_suffix_code,
                        latitude,
                        longitude,
                        geocode_type,
                        locality_pid
                    FROM {dbschema}.street_view
                    WHERE street_locality_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            try:
                cursor = config.get_db_cursor()
                # get just IDs, ordered, from the address_detail table, paginated by class init args
                cursor.execute(s)
                for record in cursor:
                    self.street_name = record[0]
                    self.street_type = record[1]
                    self.street_suffix = record[2]
                    latitude = record[3]
                    longitude = record[4]
                    geocode_type = record[5].title()
                    self.locality_pid = record[6]
                    geometry_wkt = self.make_wkt_literal(longitude=longitude,
                                                         latitude=latitude)
                    street_string = '{} {} {}'\
                        .format(self.street_name, self.street_type, self.street_suffix)
            except Exception as e:
                print(
                    "Uh oh, can't connect to DB. Invalid dbname, user or password?"
                )
                print(e)

            view_html = render_template('class_streetLocality_dct.html',
                                        identifier=self.id,
                                        title='Street ' + self.id,
                                        description=street_string,
                                        coverage=geometry_wkt,
                                        source='G-NAF, 2016',
                                        type='Street')
        else:
            return NotImplementedError(
                "HTML representation of View '{}' for StreetLocality is not implemented."
                .format(view))
        return view_html
Exemplo n.º 8
0
    def export_rdf(self, view='ISO19160'):
        g = Graph()
        a = URIRef(self.uri)

        if view == 'ISO19160':
            # get the components from the DB needed for ISO19160
            s = sql.SQL('''SELECT 
                        street_name, 
                        street_type_code, 
                        street_suffix_code,
                        latitude,
                        longitude,
                        geocode_type,
                        locality_pid
                    FROM {dbschema}.street_view
                    WHERE street_locality_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            cursor = config.get_db_cursor()
            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s)
            for record in cursor:
                ac_street_value = record[0].title()
                if (record[1] is not None):
                    ac_street_value += ' {}'.format(record[1].title())
                if (record[2] is not None):
                    ac_street_value += ' {}'.format(record[2].title())
                geometry_wkt = '<http://www.opengis.net/def/crs/EPSG/0/4283> POINT({} {})'.format(
                    record[3], record[4])
                self.locality_pid = record[6]

            AddressComponentTypeUriBase = 'http://def.isotc211.org/iso19160/-1/2015/Address/code/AddressComponentType/'
            AddressPositionTypeUriBase = 'http://def.isotc211.org/iso19160/-1/2015/Address/code/AddressPositionType/'

            ISO = Namespace(
                'http://def.isotc211.org/iso19160/-1/2015/Address#')
            g.bind('iso19160', ISO)

            GEO = Namespace('http://www.opengis.net/ont/geosparql#')
            g.bind('geo', GEO)

            g.add((a, RDF.type, ISO.Address))

            ac_street = BNode()
            g.add((ac_street, RDF.type, ISO.AddressComponent))
            g.add((ac_street, ISO.valueInformation,
                   Literal(ac_street_value, datatype=XSD.string)))
            g.add((ac_street, ISO.type,
                   URIRef(AddressComponentTypeUriBase + 'thoroughfareName')))
            g.add((a, ISO.addressComponent, ac_street))

            geometry = BNode()
            g.add((geometry, RDF.type, GEO.Geometry))
            g.add((geometry, GEO.asWKT,
                   Literal(geometry_wkt, datatype=GEO.wktLiteral)))

            position_geometry = BNode()
            g.add((position_geometry, RDF.type, ISO.GM_Object))
            g.add((position_geometry, GEO.hasGeometry, geometry))

            position = BNode()
            g.add((position, RDF.type, ISO.AddressPosition))
            g.add((position, ISO.geometry, position_geometry))
            g.add((position, ISO.type,
                   URIRef(AddressPositionTypeUriBase + 'centroid')))
            g.add((a, ISO.position, position))

        elif view == 'gnaf':
            # get the components from the DB needed for gnaf
            s = sql.SQL('''SELECT 
                        street_name, 
                        street_type_code, 
                        street_suffix_code,
                        latitude,
                        longitude,
                        geocode_type,
                        locality_pid
                    FROM {dbschema}.street_view
                    WHERE street_locality_pid = {id}''') \
                .format(id=sql.Literal(self.id), dbschema=sql.Identifier(config.DB_SCHEMA))

            cursor = config.get_db_cursor()
            # get just IDs, ordered, from the address_detail table, paginated by class init args
            cursor.execute(s)
            for record in cursor:
                self.street_name = record[0].title()
                if (record[1] is not None):
                    self.street_type = record[1].title()
                if (record[2] is not None):
                    self.street_suffix = record[2].title()
                latitude = record[3]
                longitude = record[4]
                if latitude is not None:
                    geometry_wkt = self.make_wkt_literal(latitude=latitude,
                                                         longitude=longitude)
                self.locality_pid = record[6]

            RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
            g.bind('rdfs', RDFS)

            GNAF = Namespace('http://linked.data.gov.au/def/gnaf#')
            g.bind('gnaf', GNAF)

            GEO = Namespace('http://www.opengis.net/ont/geosparql#')
            g.bind('geo', GEO)

            PROV = Namespace('http://www.w3.org/ns/prov#')
            g.bind('prov', PROV)

            DCT = Namespace('http://purl.org/dc/terms/')
            g.bind('dct', DCT)

            s = URIRef(self.uri)
            locality_uri = URIRef(config.URI_LOCALITY_INSTANCE_BASE +
                                  self.locality_pid)

            # RDF: declare Address instance
            g.add((s, RDF.type, GNAF.StreetLocality))
            g.add(
                (s, GNAF.hasName, Literal(self.street_name,
                                          datatype=XSD.string)))
            if self.street_type is not None:
                g.add(
                    (s, GNAF.hasStreetType,
                     URIRef(
                         'http://linked.data.gov.au/def/gnaf/code/StreetTypes#'
                         + self.street_type)))
            if self.street_suffix is not None:
                g.add((
                    s, GNAF.hasStreetSuffix,
                    URIRef(
                        'http://linked.data.gov.au/def/gnaf/code/StreetSuffixes#'
                        + self.street_suffix)))
#            g.add((a, GNAF.hasDateCreated, Literal(self.date_created, datatype=XSD.date)))
#            if self.date_retired is not None:
#                g.add((s, GNAF.hasDateRetired, Literal(self.date_retired, datatype=XSD.date)))
            g.add((s, GNAF.hasLocality, URIRef(locality_uri)))
            # RDF: geometry
            if geometry_wkt is not None:
                geocode = BNode()
                g.add((geocode, RDF.type, GNAF.Geocode))
                g.add((
                    geocode, GNAF.gnafType,
                    URIRef(
                        'http://linked.data.gov.au/def/gnaf/code/GeocodeTypes#Locality'
                    )))
                g.add((geocode, RDFS.label,
                       Literal('Locality', datatype=XSD.string)))
                g.add((geocode, GEO.asWKT,
                       Literal(geometry_wkt, datatype=GEO.wktLiteral)))
                g.add((s, GEO.hasGeometry, geocode))

            if hasattr(self, 'street_locality_aliases'):
                for k, v in self.street_locality_aliases.items():
                    a = BNode()
                    g.add((a, RDF.type, GNAF.Alias))
                    g.add((URIRef(self.uri), GNAF.hasAlias, a))
                    g.add((
                        a, GNAF.gnafType,
                        URIRef(
                            'http://linked.data.gov.au/def/gnaf/code/AliasTypes#Synonym'
                        )))
                    g.add((a, RDFS.label,
                           Literal(v['street_locality_name'],
                                   datatype=XSD.string)))
        elif view == 'dct':
            raise NotImplementedError(
                "RDF Representation of the DCT View for StreetLocality is not yet implemented."
            )
            # TODO: implement DCT RDF
        else:
            raise RuntimeError(
                "Cannot render an RDF representation of that View.")
        return g
Exemplo n.º 9
0
    def export_html(self, view='gnaf'):
        if view == 'gnaf':
            view_html = render_template(
                'class_locality_gnaf.html',
                locality_name=self.locality_name,
                latitude=self.latitude,
                longitude=self.longitude,
                geocode_type=self.geocode_type,
                geometry_wkt=self.make_wkt_literal(longitude=self.longitude,
                                                   latitude=self.latitude),
                state_uri=self.state_uri,
                state_label=self.state_label,
                geocode_uri=
                'http://linked.data.gov.au/def/gnaf/code/GeocodeTypes#Locality',
                geocode_label='Locality',
                alias_localities=self.alias_localities,
                locality_neighbours=self.locality_neighbours)

        elif view == 'ISO19160':
            view_html = render_template('class_locality_ISO19160.html', )

        elif view == 'dct':
            s = sql.SQL('''SELECT 
                        locality_name, 
                        latitude,
                        longitude,
                        geocode_type,
                        locality_pid,
                        state_pid
                    FROM gnaf.locality_view
                    WHERE locality_pid = {id}''') \
                .format(id=sql.Literal(self.id))

            try:
                cursor = config.get_db_cursor()
                # get just IDs, ordered, from the address_detail table, paginated by class init args
                cursor.execute(s)
                for record in cursor:
                    locality_name = record[0]
                    latitude = record[1]
                    longitude = record[2]
                    geocode_type = record[3].title()
                    locality_pid = record[4]
                    state_pid = record[5]
                    geometry_wkt = self.make_wkt_literal(longitude=longitude,
                                                         latitude=latitude)
            except Exception as e:
                print(
                    "Uh oh, can't connect to DB. Invalid dbname, user or password?"
                )
                print(e)

            view_html = render_template('class_locality_dct.html',
                                        identifier=self.id,
                                        title='Locality ' + self.id,
                                        description=locality_name,
                                        coverage=geometry_wkt,
                                        source='G-NAF, 2016',
                                        type='Locality')
        else:
            return NotImplementedError(
                "HTML representation of View '{}' for Location is not implemented."
                .format(view))
        return view_html
Exemplo n.º 10
0
    def export_rdf(self, view='gnaf', format='text/turtle'):
        g = Graph()
        a = URIRef(self.uri)

        if view == 'ISO19160':
            # get the components from the DB needed for ISO19160
            s = sql.SQL('''SELECT 
                        locality_name, 
                        latitude,
                        longitude,
                        geocode_type,
                        locality_pid,
                        state_pid
                    FROM gnaf.locality_view
                    WHERE locality_pid = {id}''') \
                .format(id=sql.Literal(self.id))

            try:
                cursor = config.get_db_cursor()
                # get just IDs, ordered, from the address_detail table, paginated by class init args
                cursor.execute(s)
                for record in cursor:
                    ac_locality_value = record[0].title()
                    latitude = record[1]
                    longitude = record[2]
                    geometry_wkt = self.make_wkt_literal(longitude=longitude,
                                                         latitude=latitude)
            except Exception as e:
                print(
                    "Uh oh, can't connect to DB. Invalid dbname, user or password?"
                )
                print(e)

            AddressComponentTypeUriBase = 'http://def.isotc211.org/iso19160/-1/2015/Address/code/AddressComponentType/'
            AddressPositionTypeUriBase = 'http://def.isotc211.org/iso19160/-1/2015/Address/code/AddressPositionType/'

            ISO = Namespace(
                'http://def.isotc211.org/iso19160/-1/2015/Address#')
            g.bind('iso19160', ISO)

            GEO = Namespace('http://www.opengis.net/ont/geosparql#')
            g.bind('geo', GEO)

            g.add((a, RDF.type, ISO.Address))

            ac_locality = BNode()
            g.add((ac_locality, RDF.type, ISO.AddressComponent))
            g.add((ac_locality, ISO.valueInformation,
                   Literal(ac_locality_value, datatype=XSD.string)))
            g.add((ac_locality, ISO.type,
                   URIRef(AddressComponentTypeUriBase + 'locality')))
            g.add((a, ISO.addressComponent, ac_locality))

            geometry = BNode()
            g.add((geometry, RDF.type, GEO.Geometry))
            g.add((geometry, GEO.asWKT,
                   Literal(geometry_wkt, datatype=GEO.wktLiteral)))

            position_geometry = BNode()
            g.add((position_geometry, RDF.type, ISO.GM_Object))
            g.add((position_geometry, GEO.hasGeometry, geometry))

            position = BNode()
            g.add((position, RDF.type, ISO.AddressPosition))
            g.add((position, ISO.geometry, position_geometry))
            g.add((position, ISO.type,
                   URIRef(AddressPositionTypeUriBase + 'centroid')))
            g.add((a, ISO.position, position))

        elif view == 'gnaf':
            RDFS = Namespace('http://www.w3.org/2000/01/rdf-schema#')
            g.bind('rdfs', RDFS)

            GNAF = Namespace('http://linked.data.gov.au/def/gnaf#')
            g.bind('gnaf', GNAF)

            GEO = Namespace('http://www.opengis.net/ont/geosparql#')
            g.bind('geo', GEO)

            PROV = Namespace('http://www.w3.org/ns/prov#')
            g.bind('prov', PROV)

            DCT = Namespace('http://purl.org/dc/terms/')
            g.bind('dct', DCT)

            l = URIRef(self.uri)

            # RDF: declare Address instance
            g.add((l, RDF.type, GNAF.Locality))
            g.add((l, GNAF.hasName,
                   Literal(self.locality_name, datatype=XSD.string)))
            g.add((a, GNAF.hasDateCreated,
                   Literal(self.date_created, datatype=XSD.date)))
            if self.date_retired is not None:
                g.add((a, GNAF.hasDateRetired,
                       Literal(self.date_retired, datatype=XSD.date)))
            g.add((l, GNAF.hasState, URIRef(self.state_uri)))
            # RDF: geometry
            geocode = BNode()
            g.add((geocode, RDF.type, GNAF.Geocode))
            g.add((
                geocode, GNAF.gnafType,
                URIRef(
                    'http://linked.data.gov.au/def/gnaf/code/GeocodeTypes#Locality'
                )))
            g.add((geocode, RDFS.label, Literal('Locality',
                                                datatype=XSD.string)))
            g.add((geocode, GEO.asWKT,
                   Literal(self.make_wkt_literal(longitude=self.longitude,
                                                 latitude=self.latitude),
                           datatype=GEO.wktLiteral)))
            g.add((a, GEO.hasGeometry, geocode))

            if hasattr(self, 'alias_localities'):
                for k, v in self.alias_localities.items():
                    a = BNode()
                    g.add((a, RDF.type, GNAF.Alias))
                    g.add((URIRef(self.uri), GNAF.hasAlias, a))
                    g.add((
                        a, GNAF.gnafType,
                        URIRef(
                            'http://linked.data.gov.au/def/gnaf/code/AliasTypes#Synonym'
                        )))
                    g.add((a, RDFS.label,
                           Literal(v['locality_name'], datatype=XSD.string)))

            if hasattr(self, 'locality_neighbours'):
                for k, v in self.locality_neighbours.items():
                    g.add((l, GNAF.hasNeighbour,
                           URIRef(config.URI_LOCALITY_INSTANCE_BASE + k)))
        elif view == 'dct':
            raise NotImplementedError(
                "RDF Representation of the DCT View for Locality is not yet implemented."
            )
            # TODO: implement DCT RDF
        else:
            raise RuntimeError(
                "Cannot render an RDF representation of that View.")
        return g
Exemplo n.º 11
0
    def __init__(self, identifier, db_cursor=None):
        self.id = identifier
        self.uri = config.URI_LOCALITY_INSTANCE_BASE + identifier

        # DB connection
        if db_cursor is not None:
            self.cursor = db_cursor
        else:
            self.cursor = config.get_db_cursor()

        self.locality_name = None
        self.latitude = None
        self.longitude = None
        self.date_created = None
        self.date_retired = None
        self.geocode_type = None
        self.geometry_wkt = None
        self.state_pid = None
        self.alias_localities = dict()
        self.locality_neighbours = dict()

        # get data from DB
        s = sql.SQL('''SELECT 
                            locality_name, 
                            l.date_created,
                            l.date_retired,
                            primary_postcode,
                            latitude,
                            longitude,
                            s.uri AS state_uri, 
                            s.prefLabel AS state_label
                        FROM gnaf.locality l
                        INNER JOIN gnaf.locality_point lp on l.locality_pid = lp.locality_pid
                        LEFT JOIN codes.state s ON CAST(l.state_pid AS text) = s.code
                        WHERE l.locality_pid = {id}''') \
            .format(id=sql.Literal(self.id))

        self.cursor.execute(s)
        rows = self.cursor.fetchall()
        for row in rows:
            r = config.reg(self.cursor, row)
            self.locality_name = r.locality_name.title()
            self.latitude = r.latitude
            self.longitude = r.longitude
            self.date_created = r.date_created
            self.date_retired = r.date_retired
            self.state_uri = r.state_uri
            self.state_label = r.state_label
            if self.latitude is not None and self.longitude is not None:
                self.geometry_wkt = self.make_wkt_literal(
                    longitude=self.longitude, latitude=self.latitude)

        s2 = sql.SQL('''SELECT locality_alias_pid, name, uri, prefLabel 
                        FROM gnaf.locality_alias 
                        LEFT JOIN codes.alias a ON gnaf.locality_alias.alias_type_code = a.code 
                        WHERE locality_pid = {id}''') \
            .format(id=sql.Literal(self.id))

        # get just IDs, ordered, from the address_detail table, paginated by class init args
        self.cursor.execute(s2)
        rows = self.cursor.fetchall()
        for row in rows:
            r = config.reg(self.cursor, row)
            alias = dict()
            alias['locality_name'] = r.name.title()
            alias['subclass_uri'] = r.uri
            alias['subclass_label'] = r.preflabel
            self.alias_localities[r.locality_alias_pid] = alias

        # get a list of localityNeighbourIds from the locality_alias table
        s3 = sql.SQL('''SELECT 
                    a.neighbour_locality_pid,
                    b.locality_name                
                FROM gnaf.locality_neighbour a
                INNER JOIN gnaf.locality_view b ON a.neighbour_locality_pid = b.locality_pid
                WHERE a.locality_pid = {id}''') \
            .format(id=sql.Literal(self.id))
        # get just IDs, ordered, from the address_detail table, paginated by class init args
        self.cursor.execute(s3)
        rows = self.cursor.fetchall()
        for row in rows:
            r = config.reg(self.cursor, row)
            self.locality_neighbours[
                r.neighbour_locality_pid] = r.locality_name.title()