Last week, my colleague asked me for a favor: "Can you help me cut out objects within this polygon from these layers and save them as a new dataset?"
Of course, I said yes. How hard could it be, right? Well, it was Friday afternoon, and my mind had already wandered off to the weekend.
There were around 40 layers, and many of them had tens of thousands of records. This was the Precisely StreetPro Display dataset for the United Kingdom.
Read on to see how it went.
Happy #MapInfoMonday!
How not to do it
I opened the tables into a map together with the polygon I should use as the cutter.
My first attempt was to select all points in the first layer within the polygon using the Boundary Select tool. That worked quickly and easily.
How to do it
With my mind back at work, we quickly came up with another strategy. Since I only needed a small subset of the objects in each table, I concluded it would be much faster to save them to a new table, then overwrite the existing table with the new one. The problem was that this involved a lot of steps where I could go wrong and overwrite the wrong table.
When overwriting existing tables, I always recommend making a copy of the data and working on that copy.
The many steps in this process prompted me to write a small script to ensure I used the correct table names when overwriting. I also built the query into the script.
The SQL window and the MapBasic window don't support loops, but you can do semi-automatic loops. This is where each run works on a new table. You do this by always using the first table opened, and when the script finishes, you make sure to close that table so another table becomes the first.
Here is the script I used:
At the beginning, I get the name and path of the first table. This is the table the script will query and overwrite.
In the middle of the script, I store the polygon in a variable and then use it in the query to select all records that intersect it. I used Intersect as the spatial operator to select the polylines and polygons that overlap only the polygon.
Finally, I save the query as a temporary table, close the current table, and then save the temporary table as the current table to overwrite it. Lastly, I close the table to make the process work on a new one.
I also added a few Print statements to write messages to the Message window. This allows me to keep track of the process and see how many records from each table have been selected.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------