the simplest answer to your problem is to convert the regions to polylines, and then
back to regions - two lines of code and Mapinfo puts the centroid in the middle where
it should be for you.
Thanks for the info on the new function, I didn't know it existed and it is somehting
that I was requesting a couple of years ago.... (and why I found the simplest way of
avoiding the issue)
R
-----Original Message-----
From: owner-mapinfo-l+AEA-lists.csn.net
+AFs-mailto:owner-mapinfo-l+AEA-lists.csn.net+AF0-On Behalf Of David M Haycraft
Sent: Thursday, April 06, 2000 10:59 AM
To: mapinfo-l+AEA-csn.net
Cc: ozsupport+AEA-mapinfo.com
Subject: MI MapBasic PointInPolygon predicate function
To MapBasic Developers
I recently undertook a small MapBasic project that turned out to be rather
more difficult than I anticipated and lead to the development of the subject
function.
The task seemed simple - create a 2D map of certain air traffic control
regions (or sectors) from an Excel spreadsheet containing info in this
format:
Region Longitude Latitude
Sector1 144.0000 12.0000
Sector1 155.0000 12.0000
Sector1 155.0000 7.3167
Sector1 154.0000 7.0000
Sector1 149.0000 7.0000
etc
Sector2 141.0000 5.0000
Sector2 141.0000 9.6167
Sector2 144.2500 9.5000
etc
Problem 1
--------------
My first effort simply used the MapBasic 5.5 statements
dim objSector as object
Create Region into variable objSector 0
Alter Region objSector Node Add (fltX1, fltY1)
Alter Region objSector Node Add (fltX2, fltY2)
etc
This code created the regions ok but had a problem. The centroid point of
each region was located at the first node of its boundary polygon, making
autolabelling ineffective, and -worse- making manual repositioning of the
centroids with the MapInfo Pro node editing tool difficult.
Problem 2
----------------
MapBasic 5.5 contains the statement
Alter Object objSector Geography OBJ+AF8-GEO+AF8-CENTROID, objPoint
although it does not appear to be documented in the on-line Help.
My second attempt was to compute the MBR of each sector
(xmin,ymin,xmax,ymax) and then try to retrospectively set the centroid
property using the statements
dim objPoint as object
create point into variable objPoint((xmin+-xmax)/2,(ymin+-ymax)/2)
OnError goto err1
Alter Object objRegion Geography OBJ+AF8-GEO+AF8-CENTROID, objPoint
err1:
The intent of this code was to set the centroid to mid-point of the region's
MBR if that point lies within, or on the border of the region, otherwise (if
the region has a concavity or hole in it) leave the centroid at its default
position on node 1.
Unfortunately the implementation of Alter Object Geography OBJ+AF8-GEO+AF8-CENTROID
seems to be faulty in that it bypasses MapBasic's error trapping mechanism,
so that, despite the OnError statement, the program simply aborted whenever
it encountered a problem region.
Problem 3
--------------
I could find no standard Mapbasic predicate function PointInPolygon that
could be used to detect the situation that causes Alter Object Geography
OBJ+AF8-GEO+AF8-CENTROID to crash and therefore avoid it.
Certainly one could use the SQL +ACI-within+ACI- operator to make this test, but the
code would be clumsy and inefficient, as you'd have to create and populate
two temporary MapInfo tables, with all the file I/O overhead involved, in
order to implement the test.
Solution
-----------
The ultimate solution was to implement a custom PointInPolygon function in
MapBasic. I used the +ACI-ray tracing+ACI- algorithm in
+ACI-Computational Geometry in C+ACI-
Joseph O'Rourke
Cambridge University Press 1993
Page 233 ff
If anybody's interested in the source code for PointInPolygon (which works
for single polygon regions without bow-tie anomalies or donut holes) then
just ask.
Regards
David M Haycraft
Information Analysis Associates Pty Ltd
ACN 085 516 105
1 Cumming Place, Wanniassa, 2903
Aust Capital Territory, Australia
Phone/Fax: 61 +- 2 +- 6231 8104
Mobile: 0412 001 134
Email: d.haycraft+AEA-acslink.aone.net.au
Web : www.acslink.aone.net.au/actaa/iaa.htm
A MapInfo Technology Partner
----------------------------------------------------------------------
To unsubscribe from this list, send e-mail to majordomo+AEA-csn.net and put
+ACI-unsubscribe MAPINFO-L+ACI- in the message body, or contact bthoen+AEA-csn.net
----------------------------------------------------------------------
To unsubscribe from this list, send e-mail to [EMAIL PROTECTED] and put
"unsubscribe MAPINFO-L" in the message body, or contact [EMAIL PROTECTED]