MapInfo Pro

 View Only

MapInfo Monday: Automating Analysis for Retail Catchment Areas

  • 1.  MapInfo Monday: Automating Analysis for Retail Catchment Areas

    Employee
    Posted 09-02-2024 08:22

    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

    Automating your Script

    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.
    Once you have inserted the variables into the script, we can run the script. This will immediately prompt you to select the tables to use via the Walking Zone Statistics dialog. Note that the OK button has been disabled until you select the Walk Zones Table. This ensures that the script doesn't continue until all the required variables have been set.
    Anyway, that was a side track. Let's finish the script.
    To join the proportional statistics back to the potential new locations, I need to save the statistics query into a temporary table and open this table. That's what the next part of script does.
    Now I can join the statistics with the potential new locations table using the Name column which goes across both tables. In the query below, you can see that I bring across all columns from both tables. I don't need them all but that was just easiest for me.
    Notice that I'm here using the last of my table variables, tabSites.
    Having joined the two tables allows me to update the potential new locations tables with values from the statistics table. That's what I do here where I assign the value from the column PropPop to the column Pop_10m. If I had calculated other statistics, I could have updated these values using the Update statement too.
    And finally, I browse the potential new locations and I close the query and temporary tables created.
    That's it.
    I marked the script as a favorite to make it appear in the Favorites list. You can find this in the SQL dropdown. You can click on a script in the list and this will run the script.
    When you click on the script, it runs and will prompt the user to specify the tables to use.
    When you click on the OK button, the script will execute the actual statements using the tables you selected.
    Below you can see the result of the script as it has been written back to the NewLocations table. You don't have to have the SQL window open to run the script.
    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
    ------------------------------