Select to view content in your preferred language

How to use Draw Map Extent to Export Map based on Users Drawing

1271
3
07-25-2018 10:24 AM
AaronPaul5
Occasional Contributor

My goal is to provide Users the ability to draw a rectangle on a map to pass the extents back to export a .png of the extent drawn.

This would be done through a 

I see potential in the Clip and Ship functionality where the User Draws a Rectangle to pass the top and bottom x,y values.

My script currently uses a County Layer to select 1 or 2 counties.

The code then zooms to selected features extent.

This is sort of hit an miss as the user needs to know what counties to select.

I want the user to draw extent instead.

In that case, the If statement would go away.

map_extent = parameters[0].valueAsText
map_extent2 = parameters[1].valueAsText
outfolder = arcpy.env.scratchWorkspace
mxd = r"\\Maps\MyMap.mxd"
if map_extent <> "Default Extent":
    expression = '"altName" IN' + " ('" + map_extent + "', '" + map_extent2 + "')"
    arcpy.AddMessage(expression)
    county_layer = arcpy.mapping.ListLayers(mxd)[0]
    arcpy.AddMessage(county_layer)
    arcpy.SelectLayerByAttribute_management(county_layer, "NEW_SELECTION", expression)
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    df.zoomToSelectedFeatures()
    arcpy.RefreshActiveView()
outputPath = outfolder + os.sep + "Map.png"
arcpy.mapping.ExportToPNG(mxd, outputPath)
arcpy.SetParameterAsText(2, outputPath)

Thank you,

Aaron

0 Kudos
3 Replies
JonathanQuinn
Esri Notable Contributor

You can probably use a combination of a Search Cursor and an extent object to set the data frame extent:

import arcpy

inGeom = arcpy.GetParameter(0)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for row in arcpy.da.SearchCursor(inGeom, ["SHAPE@"]):
    extent = row[0].extent

df.extent = extent‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

SearchCursor—Help | ArcGIS for Desktop 

Extent—ArcPy classes | ArcGIS Desktop 

AaronPaul5
Occasional Contributor

Hi Jonathan,

I would like the user to draw an extent. Not get extent from a records in a table or feature layer.

There is a built in functionality of Extract Data Tool that passes the extent in the way I'm thinking.

When using the tool in ArcCatalog, the user would have to select a data source to clip to.

In ArcMap the tool allows you to draw a rectangle to define extents.

It uses a shape called Area_of_Interest for the deault shape type.

My script will be published to ArcGIS Server, so it will be able to use a map to draw the extent.

I think if we get as far as using the draw Area_of_Extent, that would do it.

Please see screen shot of Extract Data

Thank you again,

Aaron

0 Kudos
JeffBigos
Esri Contributor

Hi Aaron, 

The parameter data type that the Area Of Interest is using above is a FeatureSet. It can be used to allow a user to either dynamically draw and input geometry (point, Line, Polygon) or browse for a Featureclass. 

The important part of that parameter in the script tool is the schema property, The FeatureClass you use for the schema defines two things for the FeatureSet:

   1. Geometry type

   2. Field type and names

As Jonathan mentioned above you can then use a cursor to get the drawn geometry from that parameter.

I have used this technique before in both Desktop (Custom Toolbox Tools) and Javascript Applications (consuming GP Services)

Thanks, 

Jeff