本文整理汇总了Python中stetl.postgis.PostGIS类的典型用法代码示例。如果您正苦于以下问题:Python PostGIS类的具体用法?Python PostGIS怎么用?Python PostGIS使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PostGIS类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pg_srs_constraint
def pg_srs_constraint(self):
log.info('set srs constraint')
db = PostGIS(self.cfg.get_dict())
srid = self.srid
sql = "ALTER TABLE gml_objects DROP CONSTRAINT enforce_srid_gml_bounded_by;"
db.tx_execute(sql)
sql = "ALTER TABLE gml_objects ADD CONSTRAINT enforce_srid_gml_bounded_by CHECK (st_srid(gml_bounded_by) = (%s));" % srid
db.tx_execute(sql)
示例2: get_feature_types
def get_feature_types(self):
log.info('reading all featuretypes from DB')
db = PostGIS(self.cfg.get_dict())
db.connect()
sql = "SELECT id,qname FROM feature_types"
db.execute(sql)
cur = db.cursor
for record in cur:
self.feature_type_ids[record[1]] = record[0]
示例3: write
def write(self, packet):
if packet.data is None:
return packet
log.info('executing SQL')
db = PostGIS(self.cfg.get_dict())
rowcount = db.tx_execute(packet.data)
log.info('executed SQL, rowcount=%d' % rowcount)
return packet
示例4: init
def init(self):
# Connect only once to DB
log.info('Init: connect to DB')
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
# Let superclass read file list from Apache URL
ApacheDirInput.init(self)
示例5: __init__
def __init__(self, configdict, section):
SqliteDbInput.__init__(self, configdict, section)
self.progress_query = self.cfg.get('progress_query')
self.progress_update = self.cfg.get('progress_update')
# Connect only once to DB
log.info('Init: connect to Postgres DB')
self.progress_db = PostGIS(self.cfg.get_dict())
self.progress_db.connect()
示例6: LmlApacheDirInput
class LmlApacheDirInput(ApacheDirInput):
"""
RIVM LML version for ApacheDirInput: adds check for each file if it is already in DB.
"""
def __init__(self, configdict, section, produces=FORMAT.record):
ApacheDirInput.__init__(self, configdict, section, produces)
self.query = self.cfg.get('query')
self.db = None
def init(self):
# Connect only once to DB
log.info('Init: connect to DB')
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
# Let superclass read file list from Apache URL
ApacheDirInput.init(self)
def exit(self):
# Disconnect from DB when done
log.info('Exit: disconnect from DB')
self.db.disconnect()
def no_more_files(self):
return self.file_index == len(self.file_list) - 1
def filter_file(self, file_name):
"""
Filter the file_name, e.g. to suppress reading if already present in DB.
:param file_name:
:return string or None:
"""
if file_name is None or file_name == 'actueel.xml':
return None
# Populate and execute SELECT query for file_name
query = self.query % file_name
rowcount = self.db.execute(query)
if rowcount > 0:
log.info('file %s already present' % file_name)
return None
# Not yet present
return file_name
示例7: init
def init(self):
# Connect only once to DB
log.info('Init: connect to DB')
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
# If no explicit column names given, get from DB meta info
self.columns = self.column_names
if self.column_names is None:
self.columns = self.db.get_column_names(self.cfg.get('table'), self.cfg.get('schema'))
示例8: init
def init(self):
# Connect only once to DB
log.info('Init: connect to DB')
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
示例9: write
def write(self, packet):
if packet.data is None:
return packet
gml_doc = packet.data
log.info('inserting features in DB')
db = PostGIS(self.cfg.get_dict())
db.connect()
# print self.to_string(gml_doc, False, False)
# NS = {'base': 'urn:x-inspire:specification:gmlas:BaseTypes:3.2', 'gml': 'http://www.opengis.net/gml/3.2'}
# featureMembers = gml_doc.xpath('//base:member/*', namespaces=NS)
featureMembers = gml_doc.xpath("//*[local-name() = '%s']/*" % self.feature_member_tag)
count = 0
gml_ns = None
for childNode in featureMembers:
if gml_ns is None:
if childNode.nsmap.has_key('gml'):
gml_ns = childNode.nsmap['gml']
else:
if childNode.nsmap.has_key('GML'):
gml_ns = childNode.nsmap['GML']
gml_id = childNode.get('{%s}id' % gml_ns)
feature_type_id = self.feature_type_ids[childNode.tag]
# Find a GML geometry in the GML NS
ogrGeomWKT = None
# gmlMembers = childNode.xpath(".//gml:Point|.//gml:Curve|.//gml:Surface|.//gml:MultiSurface", namespaces=NS)
gmlMembers = childNode.xpath(
".//*[local-name() = 'Point']|.//*[local-name() = 'Polygon']|.//*[local-name() = 'Curve']|.//*[local-name() = 'Surface']|.//*[local-name() = 'MultiSurface']")
geom_str = None
for gmlMember in gmlMembers:
if geom_str is None:
geom_str = etree.tostring(gmlMember)
# no need for GDAL Python bindings for now, maybe when we'll optimize with COPY iso INSERT
# ogrGeom = ogr.CreateGeometryFromGML(str(gmlStr))
# if ogrGeom is not None:
# ogrGeomWKT = ogrGeom.ExportToWkt()
# if ogrGeomWKT is not None:
# break
blob = etree.tostring(childNode, pretty_print=False, xml_declaration=False, encoding='UTF-8')
if geom_str is None:
sql = "INSERT INTO gml_objects(gml_id, ft_type, binary_object) VALUES (%s, %s, %s)"
parameters = (gml_id, feature_type_id, db.make_bytea(blob))
else:
# ST_SetSRID(ST_GeomFromGML(%s)),-1)
sql = "INSERT INTO gml_objects(gml_id, ft_type, binary_object, gml_bounded_by) VALUES (%s, %s, %s, ST_SetSRID( ST_GeomFromGML(%s),%s) )"
parameters = (gml_id, feature_type_id, db.make_bytea(blob), geom_str, self.srid)
if db.execute(sql, parameters) == -1:
log.error("feat num# = %d error inserting feature blob=%s (but continuing)" % (count, blob))
# will fail but we will close connection also
db.commit()
# proceed...
log.info('retrying to proceed with remaining features...')
db = PostGIS(self.cfg.get_dict())
db.connect()
count = 0
count += 1
exception = db.commit()
if exception is not None:
log.error("error in commit")
log.info("inserted %s features" % count)
return packet
示例10: delete_features
def delete_features(self):
log.info('deleting ALL features in DB')
db = PostGIS(self.cfg.get_dict())
db.tx_execute("TRUNCATE gml_objects")
示例11: init
def init(self):
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
# One time: get all device ids
self.fetch_devices()
示例12: read
def read(self, packet):
if packet.is_end_of_stream():
return packet
if self.db is None:
# First time read
log.info("reading records from blobstore..")
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
sql = self.cfg.get('sql')
self.rowcount = self.db.execute(sql)
self.cur = self.db.cursor
log.info("Read records rowcount=%d" % self.rowcount)
# Init separate connection to fetch objects referenced by xlink:href
self.xlink_db = PostGIS(self.cfg.get_dict())
self.xlink_db.connect()
# Query active
while self.cur is not None:
if self.buffer is None:
self.buffer = self.init_buf()
self.buffer.write(self.start_container)
# Get next blob record
record = self.cur.fetchone()
# End of all records
if record is None:
# End of records: start closing
self.buffer.write(self.end_container)
self.cur = None
self.db.commit()
# Only create doc if there are features in the buffer
if self.feature_count > 0:
self.buffer_to_doc(packet)
packet.set_end_of_doc()
break
else:
# New record: embed feature blob in feature tags and write to buffer
feature_blob = self.write_feature(record)
# If we have local xlinks: fetch the related features as well from the DB and
# output them within the same document (local href resolvable)
# TODO: in some cases we may need to be recursive (xlinks in xlinked features...)
# First construct a single query for all xlinks
xlink_sql = None
for xlink in self.regex_xlink_href.finditer(feature_blob):
gml_id = xlink.group(1).strip('"').strip('#')
# We don't want multiple occurences of the same xlinked feature
if gml_id in self.xlink_ids:
continue
self.xlink_ids.add(gml_id)
if xlink_sql is None:
xlink_sql = "SELECT binary_object from gml_objects where gml_id = '%s'" % gml_id
else:
xlink_sql += "OR gml_id = '%s'" % gml_id
# Should we retrieve and write xlinked features?
if xlink_sql is not None:
# Fetch from DB
self.xlink_db.execute(xlink_sql)
while True:
# Get next blob record
xlink_record = self.xlink_db.cursor.fetchone()
if xlink_record is None:
break
self.write_feature(xlink_record)
# Should we output a doc
if self.feature_count >= self.max_features_per_doc:
# End of records: create XML doc
self.buffer.write(self.end_container)
self.buffer_to_doc(packet)
break
if self.cur is None:
# All records handled: close off
packet.set_end_of_stream()
# log.info("[%s]" % packet.data)
return packet
示例13: DeegreeBlobstoreInput
class DeegreeBlobstoreInput(Input):
"""
Read features from deegree Blobstore DB into an etree doc.
produces=FORMAT.etree_doc
"""
# Start attribute config meta
@Config(ptype=int, required=False, default=10000)
def max_features_per_doc(self):
"""
Max features to read from input feature GML stream per internal document.
"""
pass
@Config(ptype=str, required=True, default=None)
def start_container(self):
"""
Tag that starts container.
"""
pass
@Config(ptype=str, required=True, default=None)
def end_container(self):
"""
Tag that ends container.
"""
pass
@Config(ptype=str, required=False, default=False)
def start_feature_tag(self):
"""
XML tag that starts Feature.
"""
pass
@Config(ptype=str, required=False, default=None)
def end_feature_tag(self):
"""
XML tag that ends Feature.
"""
pass
# End attribute config meta
def __init__(self, configdict, section):
Input.__init__(self, configdict, section, produces=FORMAT.etree_doc)
self.cur_feature_blob = None
self.rowcount = 0
# http://www.mkyong.com/regular-expressions/how-to-extract-html-links-with-regular-expression/
self.regex_xlink_href = re.compile("\\s*(?i)xlink:href\\s*=\\s*(\"#([^\"]*\")|'#[^']*'|(#[^'\">\\s]+))")
self.db = None
self.xlink_db = None
self.buffer = None
self.feature_count = 0
# Reusable XML parser
self.xml_parser = etree.XMLParser(remove_blank_text=True)
def init(self):
pass
def read(self, packet):
if packet.is_end_of_stream():
return packet
if self.db is None:
# First time read
log.info("reading records from blobstore..")
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
sql = self.cfg.get('sql')
self.rowcount = self.db.execute(sql)
self.cur = self.db.cursor
log.info("Read records rowcount=%d" % self.rowcount)
# Init separate connection to fetch objects referenced by xlink:href
self.xlink_db = PostGIS(self.cfg.get_dict())
self.xlink_db.connect()
# Query active
while self.cur is not None:
if self.buffer is None:
self.buffer = self.init_buf()
self.buffer.write(self.start_container)
# Get next blob record
record = self.cur.fetchone()
# End of all records
if record is None:
# End of records: start closing
self.buffer.write(self.end_container)
self.cur = None
self.db.commit()
# Only create doc if there are features in the buffer
if self.feature_count > 0:
#.........这里部分代码省略.........
示例14: WeewxDbInput
class WeewxDbInput(SqliteDbInput):
"""
Reads weewx raw archive records from SQLite.
"""
def __init__(self, configdict, section):
SqliteDbInput.__init__(self, configdict, section)
self.progress_query = self.cfg.get('progress_query')
self.progress_update = self.cfg.get('progress_update')
# Connect only once to DB
log.info('Init: connect to Postgres DB')
self.progress_db = PostGIS(self.cfg.get_dict())
self.progress_db.connect()
def exit(self):
# Disconnect from DB when done
log.info('Exit: disconnect from DB')
self.progress_db.disconnect()
def after_chain_invoke(self, packet):
"""
Called right after entire Component Chain invoke.
Used to update last id of processed file record.
"""
# last_datetime.datetime.fromtimestamp(self.last_id).strftime('%Y-%m-%d %H:%M:%S')
ts_local = time.strftime("%Y-%m-%d %H:%M:%S %Z", time.localtime(self.last_id))
log.info('Updating progress table ts_unix=%d ts_local=%s' % (self.last_id, ts_local))
self.progress_db.execute(self.progress_update % (self.last_id, ts_local))
self.progress_db.commit(close=False)
log.info('Update progress table ok')
return True
def read(self, packet):
# Get last processed id of archive table
self.progress_db.execute(self.progress_query)
progress_rec = self.progress_db.cursor.fetchone()
self.last_id = progress_rec[3]
log.info('progress record: %s' % str(progress_rec))
# Fetch next batch of archive records
archive_recs = self.do_query(self.query % self.last_id)
log.info('read archive_recs: %d' % len(archive_recs))
# No more records to process?
if len(archive_recs) == 0:
packet.set_end_of_stream()
log.info('Nothing to do. All file_records done')
return packet
# Remember last id processed for next query
self.last_id = archive_recs[len(archive_recs)-1].get('dateTime')
packet.data = archive_recs
# Always stop after batch, otherwise we would continue forever
packet.set_end_of_stream()
return packet
示例15: PostgresInsertOutput
class PostgresInsertOutput(PostgresDbOutput):
"""
Output by inserting single record into Postgres database.
Input is a record (Python dic structure) or a Python list of dicts (records).
Creates an INSERT for Postgres to insert each single record.
consumes=FORMAT.record
"""
def __init__(self, configdict, section, consumes=FORMAT.record):
DbOutput.__init__(self, configdict, section, consumes=[FORMAT.record_array, FORMAT.record])
self.query = None
self.db = None
self.key = self.cfg.get('key')
def init(self):
# Connect only once to DB
log.info('Init: connect to DB')
self.db = PostGIS(self.cfg.get_dict())
self.db.connect()
def exit(self):
# Disconnect from DB when done
log.info('Exit: disconnect from DB')
self.db.disconnect()
def create_query(self, record):
# We assume that all records do the same INSERT key/values
# See http://grokbase.com/t/postgresql/psycopg/12735bvkmv/insert-into-with-a-dictionary-or-generally-with-a-variable-number-of-columns
# e.g. INSERT INTO lml_files ("file_name", "file_data") VALUES (%s,%s)
query = "INSERT INTO %s (%s) VALUES (%s)" % (self.cfg.get('table'), ",".join(['%s' % k for k in record]), ",".join(["%s",]*len(record.keys())))
log.info('query is %s', query)
return query
def write(self, packet):
# Deal with empty or zero-length data structures (list or dict)
if packet.data is None or len(packet.data) == 0:
return packet
# ASSERT: record data present
# record is Python dict (single record) or list of Python dict (multiple records)
record = packet.data
# Generate INSERT query template once
first_record = record
if type(record) is list and len(record) > 0:
first_record = record[0]
# Create query once
if self.query is None:
self.query = self.create_query(first_record)
# Check if record is single (dict) or array (list of dict)
if type(record) is dict:
# Do insert with values from the single record
self.db.execute(self.query, record.values())
self.db.commit(close=False)
# log.info('committed record key=%s' % record[self.key])
elif type(record) is list:
# Multiple records in list
for rec in record:
# Do insert with values from the record
self.db.execute(self.query, rec.values())
self.db.commit(close=False)
log.info('committed %d records' % len(record))
return packet