Skip to content

Instantly share code, notes, and snippets.

@cindygis
Created July 9, 2015 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/63395728a210a94c3bfe to your computer and use it in GitHub Desktop.
Save cindygis/63395728a210a94c3bfe to your computer and use it in GitHub Desktop.
Creates a point feature class in memory containing random points within a predefined extent, and posts the points as ESRI JSON to a REST endpoint.
#
# @date 13/05/2015
# @author Cindy Williams
#
# Creates a point feature class in memory containing
# random points within a predefined extent, and posts
# the points as ESRI JSON to a REST endpoint.
#
# For use as a standalone script
#
import json
import requests
import random
import os
import arcpy
from string import ascii_uppercase
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(4326)
headers = {'Content-Type': 'application/json'}
url ="" # Insert REST endpoint here for POST
ext = "17.7556923735604 -34.8330442856367 24.2223940143152 -30.4302569614079" # Predefined extent
gdb = r"in_memory"
ftr = "randompts"
points = os.path.join(gdb, ftr)
def attrToesriJSON(x, y, name):
# Format for ESRI JSON
return {"attributes":{"Name":name}, "geometry":{"x":x,"y":y}}
arcpy.management.CreateRandomPoints(gdb, ftr, "", ext)
arcpy.management.AddField(points, "Name", "TEXT", "#", "#", 10)
with arcpy.da.UpdateCursor(points, ("SHAPE@XY", "Name")) as cursor:
for row in cursor:
# Assign a random label to each point. Not worried about repeating IDs here
row[1] = random.choice(ascii_uppercase) + str(random.randrange(1000, 10000))
cursor.updateRow(row)
# Generate the JSON
data_json = json.dumps(attrToesriJSON(row[0][0], row[0][1], row[1]))
# Post the JSON
response = requests.post(url, data=data_json, headers=headers)
# Optional: persist to disk
arcpy.conversion.FeaturesToJSON(points, r"C:\Some\Arb\Folder\randompts.json")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment