MapInfo Pro

Polygonising an Image Raster using Virtual Rasters

  • 1.  Polygonising an Image Raster using Virtual Rasters

    Pitney Bowes
    Posted 24 days ago
      |   view attached

    A client recently had cause to polygonise a raster image and sought advice on how to proceed using MapInfo Pro Advanced. As this issue has arisen before, I thought it would be worth reporting on how it was achieved in this case – using virtual rasters.

    In MapInfo Pro Advanced raster images are exposed in the interface as rasters with field type Image or ImagePalette. In this case, the client had a JPEG image that is essentially a mask. The valid pixels in the image are all one color, although there is variation due to JPEG compression noise, and a second color is used to identify empty cells. A suitable solution, in this case, was to perform a polygonisation operation using the "Valid / Invalid" rule that generates a set of polygons for the valid pixels of the raster and a second set of polygons for the invalid pixels of the raster.

    Unfortunately, the Advanced user interface does not allow you to execute a polygonisation operation on an Image field. Also, the JPEG does not distinguish between valid and invalid cells. Finally, the JPEG needs to properly spatially located.

    The client supplied a TAB file that specified three ground control points that can locate the raster. The Image driver in Advanced will load this JPEG and use the TAB file to locate it. However, I recommend using the warp tool to locate the raster by fitting an affine transformation to the supplied GCP's. Open the warp tool from the Raster Operations button on the Raster ribbon. The warp tool will automatically load the GCP's from the TAB file. Make sure you turn on the "Read SourceY as Top-down" checkbox because pixel coordinates in a JPEG are generally anchored in the top left corner and are positive downwards. Select an appropriate transform type – I used Affine – and then check the "Create MVR" option to output a virtual raster instead of a crystallised warped raster. MapInfo should display the MVR once you are done. The virtual raster is shown below.

    <VirtualRaster Version="1.0">
    	<Raster Name="Raster" File="./YourRasterImage.jpg"/>
    	<RasterOperationList Enable="True" Name="Warp">
    		<Description>MapInfo Warp Operation</Description>
    		<Warp Enable="True" Name="WarpOperation">
    			<AutoOrigin>True</AutoOrigin>
    			<AutoCellSize>True</AutoCellSize>
    			<SourceName>Raster</SourceName>
    			<FieldBandFilter>AllFieldsAndAllBands</FieldBandFilter>
    			<UnderviewInterpolation>Nearest</UnderviewInterpolation>
    			<InterpolationNearest>True</InterpolationNearest>
    			<ImageYSenseDown>True</ImageYSenseDown>
    			<WarpTransformType>Affine</WarpTransformType>
    			<GCPList>
    				<GCP>1334,844,143.926609,-40.160397</GCP>
    				<GCP>7019,976,148.233831,-40.254463</GCP>
    				<GCP>6704,4909,147.99641,-43.240508</GCP>
    			</GCPList>
    			<RasterInfo>
    				<Name>Coverage_Warp_Virtual</Name>
    				<CoordinateSystem>CoordSys Earth Projection 1, 104</CoordinateSystem>
    				<BaseTileSize>1024,1024</BaseTileSize>
    				<OverviewTileSize>1024,1024</OverviewTileSize>
    				<FieldInfo>
    				</FieldInfo>
    			</RasterInfo>
    		</Warp>
    	</RasterOperationList>
    	<RasterInfo>
    		<FieldInfo>
    			<Operation>
    				<Name>Warp</Name>
    			</Operation>
    		</FieldInfo>
    	</RasterInfo>
    </VirtualRaster>
    


    The MVR is an XML file that can be edited with any text or XML editor. We can make a simple change to the XML to convert the MVR from "Image" field type to "Continuous" field type. To achieve this, add the "Type" tag to the "FieldInfo" block and set it to "Continuous" as shown below.

    <VirtualRaster Version="1.0">
    	<Raster Name="Raster" File="./YourRasterImage.jpg"/>
    	<RasterOperationList Enable="True" Name="Warp">
    		<Description>MapInfo Warp Operation</Description>
    		<Warp Enable="True" Name="WarpOperation">
    			<AutoOrigin>True</AutoOrigin>
    			<AutoCellSize>True</AutoCellSize>
    			<SourceName>Raster</SourceName>
    			<FieldBandFilter>AllFieldsAndAllBands</FieldBandFilter>
    			<UnderviewInterpolation>Nearest</UnderviewInterpolation>
    			<InterpolationNearest>True</InterpolationNearest>
    			<ImageYSenseDown>True</ImageYSenseDown>
    			<WarpTransformType>Affine</WarpTransformType>
    			<GCPList>
    				<GCP>1334,844,143.926609,-40.160397</GCP>
    				<GCP>7019,976,148.233831,-40.254463</GCP>
    				<GCP>6704,4909,147.99641,-43.240508</GCP>
    			</GCPList>
    			<RasterInfo>
    				<Name>Coverage_Warp_Virtual</Name>
    				<CoordinateSystem>CoordSys Earth Projection 1, 104</CoordinateSystem>
    				<BaseTileSize>1024,1024</BaseTileSize>
    				<OverviewTileSize>1024,1024</OverviewTileSize>
    				<FieldInfo>
    					<Type>Continuous</Type>
    				</FieldInfo>
    			</RasterInfo>
    		</Warp>
    	</RasterOperationList>
    	<RasterInfo>
    		<FieldInfo>
    			<Operation>
    				<Name>Warp</Name>
    			</Operation>
    		</FieldInfo>
    	</RasterInfo>
    </VirtualRaster>
    


    Now, we just need to tell Advanced how to distinguish between valid and invalid cells. In this case, the valid cells were all colored red (or close to it) and the invalid cells were all white. One way to do this is to make a virtual raster copy out of our warped virtual raster and apply data conditioning to the data on the way through. Our new MVR uses the warped MVR as the raster source and declares a data conditioning object that converts white to null. As this is a JPEG, white is not always exactly white. To account for this I convert a range of values to null which catches all the white and off-white pixels whilst leaving all the red-ish pixels behind. When we connect the virtual raster to the raster source, we also link it to the named data conditioning object. Here is the MVR file.

    <VirtualRaster>
    	<DataConditioning Name = "White2Null">
    		<Range From="500000" To="16777215" Inclusive="True"/>
    	</DataConditioning>
    	<Raster File="VirtualRaster_WarpOperation_Continuous.mvr" Name="RasterSource0" />
    	<RasterInfo>
    		<FieldInfo Name="Field">
    			<BandInfo Name="Band 1">
    				<Raster>
    					<Name>RasterSource0</Name>
    					<Field>0</Field>
    					<Band>0</Band>
    					<PrimaryRaster>true</PrimaryRaster>
    					<DataConditioning Name = "White2Null" />
    				</Raster>
    			</BandInfo>
    		</FieldInfo>
    	</RasterInfo>
    </VirtualRaster>

    In MapInfo Pro Advanced 17.04, and even in the 2019 version that will be released soon, you are not permitted to use an MVR as input to a processing operation. However, we make an exception for the "convert" operation in 2019 (you can also do this in 17.04, unofficially). The next step is to use the convert operation in Advanced to convert the MVR to an MRR. Now we have a crystallised raster that we can use in the polygonisation operation in Advanced to generate polygons of the valid and invalid cells. Here is a small portion of the final MVR displayed with the polygon outlines displayed overlapping.

    Polygonisation


    ------------------------------
    Sam Roberts
    Engineer, MapInfo Pro Advanced (Raster)
    Australia
    ------------------------------

    Attachment(s)