An important part of a task that you do often is to automate it so that you can do it easier, with fewer clicks, faster, and in the same way each time.
There are different levels of automation. In this example, the process will not be completely automated. You will have to initiate the process manually. However, the business logic of the process will be automated and easy to repeat.
The task we want to automate was covered last week: MapInfo Monday: Proportional Analysis for Retail Catchment Areas.
We could automate that final query and the update statement from last week's post. But to make it more flexible, I'll also allow the user to pick the tables to use. This means the table holding the potential new site locations, the table with the walking zones, and the table with the GroundView attributes about the population in the area.
The starting point is still our idea to investigate how good potential new locations are for our business.
Happy #MapInfoMonday
We will start with the query we created in the article last week that calculates the proportional population within the walking zones around the potential new locations. Let's give this script a name that we can recognize and also make it a favorite script using the small star next to the script name.
First, we want to add the variables that will allow the user to select what tables to use.
From the Variables list, click on the Edit Variables item.
This will open the Edit Variables dialog where you can change or add new variables to your script.
To add a new variable, you can start by using the first if you haven't already used it. If you want to add a new variable, click the Add button.
For every new variable, you have to configure it. Start by selecting the type from the Type list. In our example, we need a table variable that will allow the user to select an already open table. In the list, you can see a long list of variable types such as String, Integer, Date, Layer, Window, and much more.
Next, make sure to give it a Name that is short but precise. I normally prefix my variables with an abbreviation that tells me their type. That's why I have used the name tabSites
. The abbreviation tab
refers to Table and Sites
refers to the content of the table: the potential new sites.
You must also provide a Prompt and a Help Text which will tell the user what table to select when running the automated process.
You can see that I have a specified a Default Value, NewLocations
in quotes. That is the typical name for the table with the potential new sites. The user will still be able to override this by selecting a different table but most of the time, this is the table to use.
Finally, I have used an expression to limit the number of tables listed. For the potential new sites, I know the predominant object type of the table is points. Therefore, I have limited the list to only show tables with mostly points. I use this expression:
TableInfo(ScriptContext(SCRIPT_CONTEXT_TABLEALIAS), TAB_INFO_DOMINANT_OBJECT_TYPE) = 2
Similarly, I create variables for the other two tables: the GroundView data and the Walking Zones around the potential new locations. For the last two tables, I used an expression with an additional condition. However, it is enough to check against the dominant object type. If a table isn't mappable, the dominant object type would be 0.
So stick to the expression from above but change the object type to 4 (polygons) for the other two tables.
When you have created your variables, you can find them in the Variables list, and insert them into the script by selecting them from the list.
Here is the final MapBasic script that I created.
Select wt.Name
, Sum(gv.popcy * ProportionOverlap(gv.Obj, wt.obj)) As "PropPop"
From tabWZ as "wt"
, tabGV as "gv"
Where wt.obj Intersects gv.Obj
Group By wt.Name
Into _GV_STATS NoSelect
Dim sPath As String
sPath = PathToDirectory$(TempFileName$(""))
Commit Table _GV_STATS As sPath & "_GV_STATS.tab"
Close Table _GV_STATS
Open Table sPath & "_GV_STATS.tab"
Select *
From tabSites As "site"
, _GV_STATS As "stats"
Where site.Name = stats.Name
Into _to_update NoSelect
Update _to_update
Set Pop_10m = PropPop
Browse * From tabSites
Close Table _to_update
Drop Table _GV_STATS
In the attached zip file, you can download the full MIS script to open into the SQL Window in MapInfo Pro. That will also include the table variables that I used.
I hope this gives you some inspiration to automate some of your tasks. If there is a specific task, you want some tips on automating, do reach out.
I would also like to add that if site selection is something you do a lot, you should consider our Spectrum Spatial Insights solution.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------