Sunday, 8 October 2017

mapinfo - How can I change region style in MapBasic


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

Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...