Friday, 9 November 2018

python - How to create polygons in ArcMap 10.0 using lat/long data from SQL Server 2008 database?


Forgive me if this is very basic, as I'm new to GIS work (though I've been a programmer for 10 years) and so I'm not really sure how to approach this, but here goes...


Can I take a table with xmin/xmin ymin/ymax coordinates and use it to create a bunch of polygons in ArcMap 10.0? I have a connection set up to SQL Server 2008 wherein there is a table that has several pieces of information regarding various maps that our team keeps track of.


Here's what I'm working with:


Said table contains the lat/long data in the form of xmin/xmax ymin/ymax, in addition to several other attributes - gmap_id, author, year, scale, etc. My boss asked me if I could create a connection to our SQL Server, use that information to create polygons on a map of the US, and set up a little attribute window with the other information (author, gmap_id, scale, etc) for each polygon.


I've got the first part covered, I think, thanks to this question. I set up the Data Query and pulled the columns that I initially wanted from SQLServer. That gave me a table like so:


I had a screen cap but the image won't post due to my rep < 10, so the table looks like:



gmapId, scale, usgsIdF, xmin, xmax, ymin, ymax
18015, 250000, 13139, -121.891, -123.071, 43, 45.5
18016, 125000, 13140, -117, -118.936, 34.25, 35.624
...

And I now have the data listed as a Source under the Table of Contents window.


How can I now translate that information into a bunch of squares on the map? And then can I use that to set up an attribute table?


I have a lot of python experience, so if that is the way to go I can do it. I am also good with SQL Server 2008, though I have not used it's spatial capabilities yet. So if there is a solution to be had involving TSQL I can also go that route.


I'm just scratching around trying to figure a good way to start.



Answer




Here is a snippet from a larger process that does just what you want, I believe, and should get you started:


def push_coords(coords):
"""Push a set of corner coords to featureclass record"""
cursor = arcpy.InsertCursor(output_fc)
feat = cursor.newRow()
for corner in corners:
point.X = corner[0]
point.Y = corner[1]
array.add(point)
array.add(array.getObject(0))

polygon = arcpy.Polygon(array)
feat.shape = polygon
cursor.insertRow(feat)
array.removeAll()
del feat
del cursor

point = arcpy.Point()
array = arcpy.Array()
# Corners list format: ["lowerLeft", "lowerRight", "upperRight", "upperLeft"]

cursor = arcpy.SearchCursor(input_xls)
for row in cursor:
corners = [[row.getValue(xls_lower_left_x), row.getValue(xls_lower_left_y)],
[row.getValue(xls_lower_right_x), row.getValue(xls_lower_right_y)],
[row.getValue(xls_upper_right_x), row.getValue(xls_upper_right_y)],
[row.getValue(xls_upper_left_x), row.getValue(xls_upper_left_y)]]
push_coords(corners)

This process reads columns from a Excel spreadsheet (input_xls) and then inserts the new polygon into output_fc, but you can easily adapt it use your input database data. The magic happens in push_coords, which takes a nested list of x,y corner coordinates.


No comments:

Post a Comment