Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Nombre_BBDD='test'
- host='localhost'
- puerto='5432'
- usuario='postgres'
- password='xxxxxxx'
- Nombre_Tabla='table'
- Llave_primaria='id'
- Capa_a_procesar='/xxxxxxx/shapefile.SHP'
- Tipo_geometria='POLYGON'
- capaImportar = QgsVectorLayer(Capa_a_procesar, 'capaImportar', 'ogr')
- uri = """dbname='"""+Nombre_BBDD+"""' host='"""+host+"""' port="""+puerto+""" user='"""+usuario+"""' password='"""+password+"""' key="""+Llave_primaria+""" type="""+Tipo_geometria+""" table="public".""""+Nombre_Tabla+"""" (geom) sql="""
- crs = QgsCoordinateReferenceSystem(25830)
- error = QgsVectorLayerImport.importLayer(capaImportar, uri, "postgres", crs, False, True)
- # su - postgres
- $ psql
- psql (8.4.17)
- Digite «help» to get help.
- postgres=#
- postgres=# CREATE USER your_user WITH PASSWORD 'your_password' SUPERUSER;
- CREATE ROLE
- postgres=# CREATE DATABASE utah OWNER zeito;
- CREATE DATABASE
- import psycopg2
- try:
- conn = psycopg2.connect(dbname='utah',
- host='localhost',
- port=5432,
- user='zeito',
- password='********')
- except:
- print "I am unable to connect to the database"
- cur = conn.cursor()
- layer = iface.activeLayer()
- feats = [feat for feat in layer.getFeatures()]
- ls = feats[0].geometry().asWkb().encode('hex')
- # Send it to PostGIS
- cur.execute('CREATE TABLE route(geom geometry, name text)')
- cur.execute(
- 'INSERT INTO route(geom, name)'
- 'VALUES (ST_SetSRID(%(geom)s::geometry, %(srid)s), %(name)s)',
- {'geom': ls, 'srid': 32612, 'name': 'route'})
- conn.commit() # save data
- print "Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement