Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import psycopg2
  2. from osgeo import ogr
  3.  
  4.  
  5. # connect to the database
  6. connection = psycopg2.connect(user="postgres",
  7. password="password",
  8. host="localhost",
  9. database="Datamall")
  10. # create cursor
  11. cursor = connection.cursor()
  12. cursor.execute("DROP TABLE IF EXISTS trial1")
  13. cursor.execute("CREATE TABLE trial1 (id SERIAL PRIMARY KEY, geom Geometry)")
  14. cursor.execute("CREATE INDEX trial1_index ON trial1 USING GIST(geom)")
  15. print("Successfully created ")
  16. connection.commit()
  17.  
  18. # define OSMfile path
  19. osm = ogr.Open(r"C:UsersPoonam PatelInternshipDataOSMOSMpedestrian.osm")
  20. layer = osm.GetLayer(1)
  21.  
  22. # delete the existing contents of the table
  23. cursor.execute("DELETE FROM trial1")
  24. print(str(layer.GetFeatureCount()))
  25. for i in range(layer.GetFeatureCount()):
  26. feature = layer.GetFeature(i)
  27. # Get feature geometry
  28. geometry = feature.GetGeometryRef()
  29. wkt = geometry.ExportToWkt()
  30. # Insert data into database,
  31. cursor.execute("INSERT INTO trial1 (geom) VALUES (ST_GeomFromText(" + "'" + wkt + "', 4326))")
  32. print("Data inserted successfully")
  33. connection.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement