4

I've problem with my code in MapBasic for change region style in MapBasic.

I've created voronoi and I want to select that voronoi and change the style of it to yellow border and no fill. I already try a code to change that color which I got it from other source but it takes a long time more than I change it manually from MapInfo and when I try to run it becomes not responding.

This is my code

Sub Voronoi

    Create Table "voronoi" 
        (Block_no Char(15),Remark Char(10),Type_Palm Char(10),Ha Float) 
        File "D:\voronoi.TAB" 
        TYPE NATIVE Charset "WindowsLatin1"

    Create Map For voronoi CoordSys Earth Projection 1, 104

    Create Index On voronoi (Block_no)

    Add Map Layer voronoi

    Set Map Layer 1 Editable On

    Create Object As Voronoi from sensus Into Table voronoi

    Update voronoi Set Ha = Area(obj, "sq m")

    select * from voronoi 
        where Ha >= Val(inputnumber) 
        into Selection

    Call ChangeColor

    Browse * from Selection

End Sub

Sub ChangeColor

    Fetch first from Selection
    x = 1
    Do
        oUpdateObj = Selection.obj

        b1 = Makebrush (1, 0, 16777215) '<--- Change values for your style
        p1 = MakePen(1, 2, 16776960) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update Selection 
            Set Obj = oUpdateObj 
            where RowID = x

        x = x + 1

        Fetch next from Selection
    Loop while x <= TableInfo(Selection,TAB_INFO_NROWS)

End Sub
2

You don't specify exactly where your tool get slow, but I have a few suggestions to improve the speed.

  1. Create the index on your table after you have inserted the data, not before
  2. With SQL Select statements always select into a named query and use the NoSelect keyword
  3. Add your table to the map after you have created your voronois regions.
  4. Use the Update statement and a user defined function to change the style
  5. Use Set Table ... FastEdit to avoid transaction files and speed up table editing

Implementing these suggestions will make your code look like this:

Sub Voronoi

Dim b1 As Brush,
    p1 As Pen

    Create Table "voronoi" 
        (Block_no Char(15),Remark Char(10),Type_Palm Char(10),Ha Float) 
        File "D:\voronoi.TAB" 
        TYPE NATIVE Charset "WindowsLatin1"
    Create Map For Voronoi CoordSys Earth Projection 1, 104

    Set Table Voronoi FastEdit On Undo Off
    Create Object As Voronoi from sensus Into Table voronoi
    Update voronoi 
        Set Ha = Area(obj, "sq m")
    Commit Table Voronoi

    Set Table Voronoi FastEdit On Undo Off
    select * from Voronoi 
        where Ha >= Val(inputnumber) 
        into __TO__UPDATE NoSelect

    b1 = Makebrush (1, 0, 16777215) '<--- Change values for your style
    p1 = MakePen(1, 2, 16776960) '<--- Change values for your style
    Update __TO__UPDATE
        Set OBJ = ChangeColor(OBJ, p1, b1)

    Commit Table Voronoi
    Close Table __TO__UPDATE

    Create Index On voronoi (Block_no)
    Add Map Layer voronoi
    'I have 
    'Set Map Layer 1 Editable On

    select * from Voronoi 
        where Ha >= Val(inputnumber) 
    Browse * from Selection

End Sub

Function ChangeColor( ByVal oRegion As Object
                    , ByVal penNew As Pen
                    , ByVal brsNew As Brush) As Object

    Alter Object oRegion
        Info OBJ_INFO_BRUSH, brsNew 
    Alter Object oRegion 
        Info OBJ_INFO_PEN, penNew 

    ChangeColor = oRegion

End Function
  • Thank you for your respon Mr. Peter. It's worked but give the wrong result. When i try to input hectare's value example 80 hectare the selection's browse display all value not more than 80 hectare. What's the problem? – Junita Jul 3 '15 at 7:37
  • Ah, sorry. I overlooked that. I have changed the code a bit to do another query to select the specific records only – Peter Horsbøll Møller Jul 3 '15 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.