Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import re
- def replace(matchObj):
- # Group 0 is all groups, group 1 is the first match contained in
- # parentheses. Group 2 is the whole float value.
- value = float(matchObj.group(2))
- value = value - 360
- return matchObj.group(1) + str(value) + " "
- def main():
- # connect to postgresql
- conn = psycopg2.connect("conn info")
- cur = conn.cursor()
- cur.execute("select st_astext(the_geom) from tl_2010_us_state10
- WHERE name10='Alaska'")
- # Get query results
- rows = cur.fetchall()
- alaskaText = rows[0][0]
- # The regular expression finds a positive float between 170 and 179.9,
- # inclusive. Matches any character not a negative sign to ensure that the
- # longitude value is positive, then it matches the rest of the number.
- # To ensure that the number matched is longitude, we check that it is
- # followed by a space (the format being "longitude latitude,".
- matchText = re.compile('([^-])(1[67][0-9].[0-9]*) ')
- # Substitute longitude values
- alaskaText = matchText.sub(replace, alaskaText)
- # insert into dabase and commit transaction
- cur.execute("insert into tl_2010_us_state10 (the_geom, name10)
- VALUES (st_geomfromtext('%s', 4269), 'Alaska2')" %(alaskaText))
- conn.commit()
- conn.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement