You can, as I mentioned, also take this to yet another level using a function.
Include "MAPBASIC.DEF"
Declare Sub Main
Declare Function GetMostOverlappingName(ByVal oPolygon As Object) As String
Dim t1 as String
Dim t2 as String
Dim MostName as String
'''''''''''''''''''''''''''''''''''''''
Sub Main
Dim tStart As Time
t1 = Input("Main object table", "_Table:", "table", "TableInfo(ScriptContext(2)")
If Not CommandInfo(CMD_INFO_DLG_OK) then Note "Exiting!" End Program Else End if
t2 = Input("Second object table", "_Table:", "table", "TableInfo(ScriptContext(2)")
If Not CommandInfo(CMD_INFO_DLG_OK) then Note "Exiting!" End Program Else End if
tStart = CurTime()
Update t1
Set Most_In = GetMostOverlappingName(OBJ)
'''''''''''''''''''''''''''''''''''''
Note "Completed: " & (CurTime() - tStart) & " secs"
End Program
End Sub
Function GetMostOverlappingName(ByVal oPolygon As Object) As String
GetMostOverlappingName = ""
Select Name, AreaOverlap(obj, oPolygon) "Overlap"
from t2
Where Obj partly within oPolygon
Order By Overlap
into _MostName NoSelect Hide
If TableInfo(_MostName, TAB_INFO_NROWS) > 0 Then
Fetch First from _MostName
MostName = _MostName.Name
Close Table _MostName
GetMostOverlappingName = MostName
Else
Close Table _MostName
End If
End Function
PS: I made a typo in one of the Select statements in my first reply:
Select Name, AreaOverlap(_OVERLAPS.obj, t1obj) "Overlap"
should have been:
Select Name, AreaOverlap(obj, t1obj) "Overlap"
I have changed it in my first reply as well.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data
------------------------------
Original Message:
Sent: 05-21-2025 10:02
From: Ryan Cook
Subject: Region that another region is most inside
Incredible. It's gone from updating one row every 10-20 seconds to updating ~100 rows per second. I didn't realise you could stick any function into the select expression list - that might be a game-changer!
Also disappointed in myself for not spotting the far more streamlined and obvious sort method for getting the max value rather than faffing about with all that maxval stuff.
Thanks again Peter!
------------------------------
Ryan Cook
ORH LTD
------------------------------
Original Message:
Sent: 05-21-2025 09:16
From: Ryan Cook
Subject: Region that another region is most inside
Thanks Peter, I will give your suggestions a whirl!
------------------------------
Ryan Cook
ORH LTD
Original Message:
Sent: 05-21-2025 07:54
From: Peter Møller
Subject: Region that another region is most inside
I would suggest eliminating some of the Select statements and the Update statement.
Change this:
Print "Finding overlaps..."Select * from t2 where obj partly within t1Obj into _OVERLAPS Print "Calculating area overlap..."Update _OVERLAPS set AreaOv = AreaOverlap(_OVERLAPS.obj, t1obj) Print "Finding max..."Select Max(AreaOv) as "Max" From _OVERLAPS into _MAXLAPFetch First from _MAXLAPMaxVal = _MAXLAP.Max Print "Finding most..."Select Name from _OVERLAPS where AreaOv = MaxVal into _MostNameFetch First from _MostNameMostName = _MostName.Name
to this:
Print "Finding overlaps, calculating overlap, and sorting by overlap..."Select Name, AreaOverlap(_OVERLAPS.obj, t1obj) "Overlap"from t2 where obj partly within t1Obj Order By Overlapinto _MostName NoSelect Hide Fetch First from _MostNameMaxVal = _MostName.OverlapMostName = _MostName.Name
I would also recommend getting the RowID from the current record you are processing, and using this when updating:
t1alias = t1 + ".RowID"t1RowID = t1alias...Print "Updating table with most..."Update t1 set Most_In = MostName where RowID = t1RowID
Note that I added Hide to your Select statement. This will hide the temporary query from the Table List and so save time refreshing the screen.
Finally, you could convert this into a function and use this to update the Name column in your t1 table:
Update t1 Set Name = GetMostOverlappingName(OBJ)
The function GetMostOverlappingName takes the obj from t1 as a parameter and will return the name of the most overlapping polygon from t2. The content of the function will be similar to the changed example I gave above.
------------------------------
Peter Horsbøll Møller
Principal Presales Consultant | Distinguished Engineer
Precisely | Trust in Data