Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. Nombre_BBDD='test'
  2. host='localhost'
  3. puerto='5432'
  4. usuario='postgres'
  5. password='xxxxxxx'
  6. Nombre_Tabla='table'
  7. Llave_primaria='id'
  8. Capa_a_procesar='/xxxxxxx/shapefile.SHP'
  9. Tipo_geometria='POLYGON'
  10.  
  11. capaImportar = QgsVectorLayer(Capa_a_procesar, 'capaImportar', 'ogr')
  12. uri = """dbname='"""+Nombre_BBDD+"""' host='"""+host+"""' port="""+puerto+""" user='"""+usuario+"""' password='"""+password+"""' key="""+Llave_primaria+""" type="""+Tipo_geometria+""" table="public".""""+Nombre_Tabla+"""" (geom) sql="""
  13. crs = QgsCoordinateReferenceSystem(25830)
  14. error = QgsVectorLayerImport.importLayer(capaImportar, uri, "postgres", crs, False, True)
  15.  
  16. # su - postgres
  17. $ psql
  18. psql (8.4.17)
  19. Digite «help» to get help.
  20.  
  21. postgres=#
  22.  
  23. postgres=# CREATE USER your_user WITH PASSWORD 'your_password' SUPERUSER;
  24. CREATE ROLE
  25. postgres=# CREATE DATABASE utah OWNER zeito;
  26. CREATE DATABASE
  27.  
  28. import psycopg2
  29.  
  30. try:
  31.  
  32. conn = psycopg2.connect(dbname='utah',
  33. host='localhost',
  34. port=5432,
  35. user='zeito',
  36. password='********')
  37.  
  38. except:
  39. print "I am unable to connect to the database"
  40.  
  41. cur = conn.cursor()
  42.  
  43. layer = iface.activeLayer()
  44. feats = [feat for feat in layer.getFeatures()]
  45. ls = feats[0].geometry().asWkb().encode('hex')
  46.  
  47. # Send it to PostGIS
  48. cur.execute('CREATE TABLE route(geom geometry, name text)')
  49. cur.execute(
  50. 'INSERT INTO route(geom, name)'
  51. 'VALUES (ST_SetSRID(%(geom)s::geometry, %(srid)s), %(name)s)',
  52. {'geom': ls, 'srid': 32612, 'name': 'route'})
  53.  
  54. conn.commit() # save data
  55.  
  56. print "Done"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement