AppleScript can make life much easier when performing repetitive tasks. Recently we had an issue with a customer needing to automate the Count Points in Polygons tool across a number of point layers. The process was taking a long time due to the sheer number of point layers being aggregated. To automate the process we wrote a script that will count points from multiple point layers and automatically add new data fields to the polygon layer. The new data fields will be labeled to match the name of the point layer being aggregated.
Here, I'll show an example of this using crime data from from Washington D.C. (The data are available for download at DC GIS). We'll start with the crime data, downloaded from DC GIS, and block group polygons, downloaded from the U.S. Census Bureau. Import the data by choosing File > Import Vector Data. Note that if you download the crime data from DC GIS you will need to create new layers from selections for each of the crime types. Also, note that you can use the script in the example below on any set of point and polygon layers. So if you have point layers you want aggregated to one of your own polygons layers then you can use the script in the exact same way as shown in the example.That is, the script is not written only to be used with data in the example below.
Cartographica offers a couple ways to complete this task of aggregating points to polygon features. First, you can choose Tools > Counts Points in Polygons. When you have a large number of point layers you will need to perform this task repetitively for each of the point layers that you would like to have aggregated to the polygon layer. When you have many point layers to aggregate, this process can be time consuming. The second option is that you can choose to perform the task by running an AppleScript that will automatically perform the task for you.
Running the Script
The script is available by the following the link.
To run the script
1. Make sure your Map window is frontmost in Cartographica
2. Double-click on the script file to open AppleScript Editor
The script we have provided has two parts needing mention. The first part is the script for the Standard Utilities. The Standard Utilities are some additional functions that you can copy and use in your own scripts to handle choosing layers from a list, creating unique layer and column names, and doing numeric conversion.
The second part is at the bottom of the script where there are commands that performs the repetitive count points in polygons task. The commands are shown below. The first set command tells Cartographica to create a selection box for the point layer that you want aggregated. The second set of commands tells Cartographica to create a selection box fot the polygon layers that you want to have points aggregated on. The final command repeats the process for all selected point layers and adds a count field to the selected polygon layer that contains the original point layer's name.
tell application "Cartographica"
set selectedLayers to my chooseLayers("Choose the Point layers", the document of the front window, 1, yes)
set chosenLayers to my chooseLayers("Choose the Polygon layer", the document of the front window, 5, no) set polyLayer to item 1 of chosenLayers
repeat with targetlayer in selectedLayers
add point count polyLayer counting targetlayer column name (name of targetlayer
3. Click Run
After clicking run the program will prompt you with selection boxes. Be sure to select the point layer like shown in the image below. Once you select the point layers click ok.
After you select the point layers you will be prompted to select the polygon layer that you want the points aggregated to. Select the polygon layer and then click ok.
The result of running the code should be new fields with names corresponding to the aggregated layer. See below for an example.