Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import psycopg2
  2. import re
  3.  
  4. def replace(matchObj):
  5. # Group 0 is all groups, group 1 is the first match contained in
  6. # parentheses. Group 2 is the whole float value.
  7. value = float(matchObj.group(2))
  8. value = value - 360
  9. return matchObj.group(1) + str(value) + " "
  10.  
  11. def main():
  12. # connect to postgresql
  13. conn = psycopg2.connect("conn info")
  14. cur = conn.cursor()
  15. cur.execute("select st_astext(the_geom) from tl_2010_us_state10
  16. WHERE name10='Alaska'")
  17.  
  18. # Get query results
  19. rows = cur.fetchall()
  20. alaskaText = rows[0][0]
  21.  
  22. # The regular expression finds a positive float between 170 and 179.9,
  23. # inclusive. Matches any character not a negative sign to ensure that the
  24. # longitude value is positive, then it matches the rest of the number.
  25. # To ensure that the number matched is longitude, we check that it is
  26. # followed by a space (the format being "longitude latitude,".
  27. matchText = re.compile('([^-])(1[67][0-9].[0-9]*) ')
  28.  
  29. # Substitute longitude values
  30. alaskaText = matchText.sub(replace, alaskaText)
  31.  
  32. # insert into dabase and commit transaction
  33. cur.execute("insert into tl_2010_us_state10 (the_geom, name10)
  34. VALUES (st_geomfromtext('%s', 4269), 'Alaska2')" %(alaskaText))
  35. conn.commit()
  36. conn.close()
  37.  
  38. if __name__ == "__main__":
  39. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement