MapInfo Pro

  • 1.  MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-25-2021 05:01
    Edited by Peter Horsbøll Møller 10-26-2021 12:27

    Happy #MapInfoMonday!

    I got a question​ last week from @Dennis Coppens. He couldn't remember how to manually move the centroid for his polygon layer. Here's a brief walk-through of how to do this.

    Do note that this is only possible for polygons. For points, lines, polylines, and other object types, the centroid is automatically calculated and isn't stored with the object.

    One example of why you may want to move the centroid of a polygon is to control the location of the labels. Typically, labels are located at the centroid of the polygon. Below you can see a map of municipalities of The Nederlands. Most labels are nicely placed inside the polygons but a few are located in a way that makes them cross into neighboring polygons.


    Here I have selected one polygon where the label could be placed a bit better inside the polygon.


    Please note that before starting the manually move all the centroids of your polygons, try out some of the label placement improvements we made a while back. From the Labels tab, check out the Label Rules dropdown.


    Or check out the Labelling Rules tab from the Layer Properties dialog.


    Note that the behavior of these depends on the object types in the table.

    Manually moving the centroid of a polygon

    Anyway, back to the main idea behind this article: Moving the centroid of a polygon.

    The first thing you need to do is to turn on Show Centroids for the layer via the Style tab. Make sure you have selected the layer that you want to edit the centroids on.


    If you have labels turned on, it can be hard to see the centroids as the labels are positioned right on top of the centroids. In the map below, I have turned off the labels so that you better can see the blue dots representing the centroids for the polygons.


    If you have labels turned on to be able to see the new location of these as you move the centroids, I'd recommend that you make the labels not selectable. This prevents you from selecting the labels instead of the centroids.

    Now make the layer editable, select a polygon and turn on Reshape. You can activate Reshape from the Map Mini Toolbar or via the keyboard shortcut Ctrl + R. You can also access Reshape from the Nodes dropdown on the Spatial tab.

    Reshape allows you to change the form of the polygon by moving existing nodes and adding new nodes. When Show Centroids is activated, it's also possible to move the centroid. Simply hold the mouse on top of the centroid click and hold down your left mouse key, drag the centroid to the new position and release the mouse key.


    When you release the mouse key, the centroid is moved, and if the layer is configured to place the label on top of the centroid, the label is also repositioned as you can see below.


    ​Do note that a centroid needs to be placed inside the polygon. You can not drag it outside of the polygon. If you try, you will see this error.


    Remember to save the changes to the table.


    I hope you have found this article useful. Have you ever moved the centroids of polygons? If yes, can you share why you did so?



    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------


  • 2.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Employee
    Posted 10-26-2021 08:18
      |   view attached
    Attaching a tool created by Bill Thoen back in 2010 that will help locate the 'Center of Gravity' of a Polygon.  Feel free to modify source code as needed.

    ------------------------------
    Bill Wemple
    Principal Software Engineer, Quality Management
    Precisely, Inc
    precisely.com
    ------------------------------

    Attachment(s)

    zip
    CenterOfGravity.zip   11 KB 1 version


  • 3.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-26-2021 08:20
    Good old Bill Thoen. Nice to see him mentioned!

    ------------------------------
    Steve Wallace
    Florida Farm Bureau Insurance
    ------------------------------



  • 4.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-26-2021 08:22
    I like this, but it makes my brian itch. Surely you can't technically move a centroid, it's a mathematical value? What you're actually doing is using a fake centroid and calling it a centroid? Which doesn't mean it isn't useful!

    ------------------------------
    Martin Burroughs
    Oldham Council
    ------------------------------



  • 5.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-26-2021 08:30
    You are correct. MapInfo Pro does not create a true center-of-mass centroid. From what I know of their process, it is the center of the minimum bounding rectangle of the object. If that point happens to fall outside the object (perhaps it is a crescent or doughnut shape), then the point is moved laterally until it falls within the object.

    ------------------------------
    Steve Wallace
    Florida Farm Bureau Insurance
    ------------------------------



  • 6.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-26-2021 15:02
    > Have you ever moved the centroids of polygons? If yes, can you share why you did so?

    "SiteMapper" is a batch tool for exporting map images, with a map created for each record in a table.  The tool is available from the MapInfo Marketplace at http://mapinfomarketplace.precisely.com/product/5cec1cc8-ff43-4fee-8eb5-2c5da9e417f7

    The tool can use the polygon centroid to centre the map for each record (allowing you to keep the map scale constant for all maps).  This works well when all objects are about the same size, and the centroids are at the centre of the Minimum Bounding Rectangle.

    However, it can give poor results if the centroid is not near the centre.  For example, if the polygon was created by enclosing a polyline object, then the centroid may end up on the edge of the polygon.  In this case, you may want to move the centroid, for example to the centre of the MBR or the 'centre of gravity'.

    If your table contains many records where the centroid needs moving, then doing it by hand can take too long.  If your table already contains X and Y columns containing the position you want to move the centroid to, then what is the easiest way of updating the centroid automatically please (either using MapBasic or the user interface)?



  • 7.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-27-2021 08:01
    Peter,

    I'm sure you probably know this but you can change the centroids location within a MIF file when you export/import layers.

    Years ago at Dataview one of our developers wrote a little mbx that exported a large polygon layer to MID/MIF format. I believe (although others may correct me here) that row 1 in the MID file is always the first region in the MIF file and so on.  The tool then read the desired new X/Y from the MID file and then overwrote the centroid in the MIF file and then re-imported the data into a new  .tab file.

    The centroid co-ords are the last line of data for each polygon record in the MIF file before the next region info is shown.


    Unfortunately I don't have any of the code and it was probably written for v7 anyway!

    HTH

    Nick

    ------------------------------
    Nick Hall
    Mapchester LTD
    Manchester
    ------------------------------



  • 8.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 11-03-2021 05:29
    Peter,

    You can use the Alter Object statement to modify the centroid of a polygon through MapBasic:

    Alter Object oPolygon Geography OBJ_GEO_CENTROID, CreatePoint(fX, fY)

    This is a bit too complex to run through the MapBasic or SQL window as you need to fetch and get the object from a record, update the object via the statement above, and then update the current record with the changed object.

    ------------------------------
    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data
    ------------------------------



  • 9.  RE: MapInfo Monday: Moving the Centroid for Polygons

    Posted 10-26-2021 15:06
    >> Have you ever moved the centroids of polygons? If yes, can you share why you did so?

    Before MapInfo got better label tools, I used to move centroids around in certain layers I used often, so the automatic labels would be positioned better inside the regions.


    ------------------------------
    Steve Wallace
    Florida Farm Bureau Insurance
    ------------------------------