ArcXML/ArcIMS equivalents in python/arcpy - creating points and lines

2788
6
Jump to solution
11-25-2015 09:22 AM
DavidChevrier
Occasional Contributor II

I need to update an old script that uses ArcIMS and ArcXML into modern day python and arcpy.  This script runs in perl, but the part I need to recreate has to do with creating graphics layers on the fly.  Given some lat/longs, how do I create them as points, then draw lines between them, and then symbolize them how I want?

In perl, I create the xml I want, then send it to the server pretty easily as seen below:

In this code I create the points and lines based on an array of lat/longs

forum1.JPG

Then I send the request to the arcims server which is hosting a basemap that these are drawn on and export a jpeg:

forum2.JPG

This seems much harder than it should be in python.  The script I've come up with so far is below.  It creates a points layer and adds it to an existing map (which works fine), but I cant change the symbols.  Basically I just want to use a text letter for each point.  The other tricky part will be creating the lines from it.  I will use the script and tool in arctoolbox, but this is really a ton more work and I still don't know how to change the symbols.  The only idea I came up with is to add a dummy layer that is symbolized how I want but turned off, then apply that symbology to the newly created letters.  But that won't give me the dynamic creation I need.  (These scripts do a bunch more, I just tried to simplify it here.)  At the end it exports a jpeg of the map.

import arcpy

import os

import uuid

arcpy.env.overwriteOutput = True

templateMxd = r"C:\location\to\my\mxd.mxd"

mxd = arcpy.mapping.MapDocument(templateMxd)

# list of lat/longs

ptList = [[-82.1, 29.51], [-80.07, 31.13], [-82.97, 31.96], [-86.12, 27.71], [-82.9, 25.25]]

pt = arcpy.Point()

ptGeoms = []

for p in ptList:

    pt.X = p[0]

    pt.Y = p[1]

    ptGeoms.append(arcpy.PointGeometry(pt))

   

tempPoints = os.path.join(arcpy.env.scratchGDB, 'tempPoints')

arcpy.env.workspace = arcpy.env.scratchFolder

arcpy.CopyFeatures_management(ptGeoms, tempPoints)

arcpy.MakeFeatureLayer_management(tempPoints, "Start_Points")

arcpy.SaveToLayerFile_management("Start_Points", "Start_Points.lyr")

tempPointsLayer = os.path.join(arcpy.env.scratchFolder, 'Start_Points.lyr')

addLayer = arcpy.mapping.Layer(tempPointsLayer)

df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]

arcpy.mapping.AddLayer(df, addLayer, "TOP")

lyrExtent = addLayer.getSelectedExtent()

df.extent = lyrExtent

output = 'WebMap_{}.jpg'.format(str(uuid.uuid1()))

Output_File = os.path.join(arcpy.env.scratchFolder, output)

# Export the WebMap

arcpy.mapping.ExportToJPEG(mxd, Output_File, df, df_export_width=500, df_export_height=500, resolution=150)

# Set the output parameter to be the output file of the server job

arcpy.SetParameterAsText(1, Output_File)

del mxd, addLayer, lyr

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

I would add the letter as an attribute and label rather than symbolize. So, if the letter in the attribute for a feature was "P" it would be labeled as "P". Do you have a more complex symbol in mind, like a letter on top of a circle or something?

View solution in original post

6 Replies
DarrenWiens2
MVP Honored Contributor

I think you can achieve this by using an InsertCursor to create the point geometries plus an attribute for the letter, rather than the list/CopyFeatures method. Your insert cursor would use fields like: ['LETTER', 'SHAPE@XY']

As you suggest, you'd need a layer symbolized as you'd like, but I think this would be best done with no symbol and labeling the point with the letter.

DavidChevrier
Occasional Contributor II

Thanks!  That will help with the table create and running the tools I need.  Any idea about changing the symbology after I add the points to the map to be a letter rather than the default circle?

0 Kudos
DarrenWiens2
MVP Honored Contributor

I would add the letter as an attribute and label rather than symbolize. So, if the letter in the attribute for a feature was "P" it would be labeled as "P". Do you have a more complex symbol in mind, like a letter on top of a circle or something?

DavidChevrier
Occasional Contributor II

Can't believe I hadn't thought of that...  thank you!!  One last question though, how do I remove the default symbol it will give me?  Just make a dummy layer in the map with no symbology and assign my new point layer to that?  I still need to know how to change the symbology of the line layer that is created with this script 2.

0 Kudos
DavidChevrier
Occasional Contributor II

For the line symbol, it is a basic line, I just need to assign them different colors in code

0 Kudos
DarrenWiens2
MVP Honored Contributor
Just make a dummy layer in the map with no symbology and assign my new point layer to that?

Yes, you can switch data source of your dummy layer ​to your new layer using the replaceDataSource method.

I'm not sure how to dynamically color your new lines. You may want to open a new question for that.

0 Kudos