Advertisement
Guest User

Untitled

a guest
Jul 28th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #
  2. # @date 13/05/2015
  3. # @author Cindy Williams
  4. #
  5. # Creates a point feature class in memory containing
  6. # random points within a predefined extent, and posts
  7. # the points as ESRI JSON to a REST endpoint.
  8. #
  9. # For use as a standalone script
  10. #
  11.  
  12. import json
  13. import requests
  14. import random
  15. import os
  16. import arcpy
  17. from string import ascii_uppercase
  18.  
  19. arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(4326)
  20.  
  21. headers = {'Content-Type': 'application/json'}
  22. url ="" # Insert REST endpoint here for POST
  23. ext = "17.7556923735604 -34.8330442856367 24.2223940143152 -30.4302569614079" # Predefined extent
  24. gdb = r"in_memory"
  25. ftr = "randompts"
  26. points = os.path.join(gdb, ftr)
  27.  
  28. def attrToesriJSON(x, y, name):
  29. # Format for ESRI JSON
  30. return {"attributes":{"Name":name}, "geometry":{"x":x,"y":y}}
  31.  
  32. arcpy.management.CreateRandomPoints(gdb, ftr, "", ext)
  33. arcpy.management.AddField(points, "Name", "TEXT", "#", "#", 10)
  34.  
  35. with arcpy.da.UpdateCursor(points, ("SHAPE@XY", "Name")) as cursor:
  36. for row in cursor:
  37. # Assign a random label to each point. Not worried about repeating IDs here
  38. row[1] = random.choice(ascii_uppercase) + str(random.randrange(1000, 10000))
  39. cursor.updateRow(row)
  40. # Generate the JSON
  41. data_json = json.dumps(attrToesriJSON(row[0][0], row[0][1], row[1]))
  42. # Post the JSON
  43. response = requests.post(url, data=data_json, headers=headers)
  44.  
  45. # Optional: persist to disk
  46. arcpy.conversion.FeaturesToJSON(points, r"C:\Some\Arb\Folder\randompts.json")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement