This project is read-only.

Select pixels of a raster layer

Jan 22, 2013 at 8:58 AM

Hi everybody

I have a problem selecting pixels of a raster layer.

I've got a raster layer, then the user draws a polygon by clicking mouse button. This polygon is converted to a polygon shape layer.

I've tried to convert every pixel in a square shape item, then I used "DotSpatial.Topology.IGeometry.Intersects" to know if this squared shape is into the polygon, but it costs a lot....

Do you know how could I select the pixels that are into the polygon????

Thank you so much!!!!

Jan 22, 2013 at 9:58 AM

so you have an input raster (lets call it inraster) and a polygon.

did you try to convert your polygon to a grid?

using the DotSpatial.Analysis.VectorToRaster.ToRaster you can convert your shape to a raster map (let's call it polygrid).

In this process you may assign to the cells of polygrid a value of 1 if they are inside and nodata outside the polygon (this is automatic using the toraster method above).

It will take some time to convert the polygon, but not so long comparing to the process you depicted above.

Once you have the polygrid raster you may cycle through rows and cols and when you find a value=1 you know you are in the inside of your polygon so you can analyze the corresponding data in the same position of your inraster .

Then you have to be careful that your new grid layer may probably have a different extent than the raster you want to analyze, so cell index in polygrid may not correspond to cell index in inraster.

So for each point inside the polygon, to recover the inraster data you may have to use the actual cell position in polygrid (use an rcindex variable) and convert it to coordinate with celltoproj function, then you can use such coordinate to recover cell index in inraster using its projtocell function.


I hope it is clear enough.

Let me know if you need some coding to show you how to do it (but I won't answer very quickly these days)



Jan 22, 2013 at 10:14 AM

Thank you Oscar, I will try it.


Jan 23, 2013 at 9:59 AM
Edited Jan 23, 2013 at 9:59 AM

Hi again, i tried to do what you said Oscar, but i have an error.


This is the code i wrote


Private _PolySelectionLayer As MapPolygonLayer

 _FSPolygon = New FeatureSet(FeatureType.Polygon)
                        _FSPolygon.Features.Add(New DotSpatial.Topology.Polygon(_coordinates))
                        _PolySelectionLayer = New MapPolygonLayer(_FSPolygon)


For Each fet As IFeature In _PolySelectionLayer.DataSet.Features
                newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(fet, cellSize, "ID", "Selected")

When I call "DotSpatial.Analysis.VectorToRaster.ToRaster" with fet as IFeature, compiler shows me an error that says tha it is unable to cast Feature to IFeature.

How could i resolve this?

Thank You



Jan 23, 2013 at 10:49 AM

1) The first argument should be an ifeatureset, not an ifeature

2) I don't understand why you  convert each feature one by one? do you need to do so?

3)In my project I used to work with mappolygonlayer interface (imappolygonlayer) to declare 


but maybe it does not matter if you leave mappolygonlayer instead of imappolygonlayer

So you may declare a variable

feats as ifeatureset= _polyselectionlayer.dataset

and convert feats

beware that you may need to add an additional field to the feats datatable to which you assign the value to be converted (it might be 
a temporary field that you delete once you have converted feats) because I had some problem using the "ID" field... don't know why

Hope it serves
Jan 28, 2013 at 9:29 AM

Hi Again

I tried to do what you said above...

I've got this:

Dim newRaster As DotSpatial.Controls.IMapRasterLayer

 Dim feats As IFeatureSet = _PolySelectionLayer.DataSet
 Dim column As New DataColumn("Ide")

 newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", "Selected")


And I've got an error that says "The specified file type is not supported"


_PolySelectionLayer is defined as mappolygonlaye, which has the polygon Shape...


I don't know why this error is shown.

Can someone help me?




Jan 28, 2013 at 9:35 AM

Try to create the new DataColumn by specifying the datatype


New DataColumn("Ide", GetType(Int32))


It should work


Jan 28, 2013 at 9:51 AM
Edited Jan 28, 2013 at 9:51 AM

Hi, I did it and now i have this error: "Missing operand after '=' operator." when i call VectorToRaster


Jan 28, 2013 at 10:02 AM

don't you miss some parameters when calling vectortoraster?

I placed the following in my code:

        Dim opt(1) As String       

opt(0) = ""

DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellsize, "Ide", Fileout, "", opt, Nothing)


the last parameter is for progresshandler which I set to nothing


Jan 28, 2013 at 10:09 AM

Hi Oscar,

I placed that options array in my code, then call the vectorToraster like this:

 Dim feats As IFeatureSet = _PolySelectionLayer.DataSet
 Dim column As New DataColumn("Ide", GetType(Int32))
 Dim opt(1) As String
 opt(0) = ""


newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", "Selected", "", opt, Nothing)

but still I'm having the same error "Missing operand after '=' operator."

I'm giving no value to de column "Ide", should the error came from here?




Jan 28, 2013 at 10:16 AM

well, that's strange.

Did you set a value to the new added column?

if not, try with this code (place the value you want, I placed 1 just to make an example)

        For i As Integer = 0 To feats.Features.Count - 1
            feats.Features(i).DataRow("Ide") = 1


Hope this solves your problem because I have no other ideas where to search for the error


Jan 28, 2013 at 10:25 AM
Edited Jan 28, 2013 at 10:26 AM

I gave value to de new column, and now i have "The specified file type is not supported" error.

this is the part of the code i'm trying to use

Dim feats As IFeatureSet = _PolySelectionLayer.DataSet
Dim column As New DataColumn("Ide", GetType(Int32))
Dim opt(1) As String
opt(0) = ""
opt(1) = ""

For n As Integer = 0 To feats.Features.Count - 1

  feats.Features(n).DataRow("Ide") = 1


newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", "Selected", "", opt, Nothing)

I've no idea why this error hapens, feats is an IfeatureSet object, the other paramaters are just integers, strings, and string array...



Jan 28, 2013 at 10:42 AM

what is "Selected"?

you should put a name for the rasterfile, I think it should be a .bgd filename (e.g. "selected.bgd")

Try it


Jan 28, 2013 at 11:00 AM

Hi again,

When i try it i have this error "Unable to cast object of type 'DotSpatial.Data.BgdRaster`1[System.Int32]' to type 'DotSpatial.Controls.IMapRasterLayer'."

this is the call: "newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", "Selected.bgd", "", opt, Nothing)"

But if I call with this arguments:

Dim newRaster As DotSpatial.Controls.IMapRasterLayer = Nothing

"DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", newRaster, "", opt, Nothing)"

i have this error: "Object reference not set to an instance of an object.", how could i initialize an imaprasterLayer?




Jan 28, 2013 at 11:16 AM

newRaster should be declared as an IRaster

then, after you declared it IRaster, try both with and without .bgd extension (not sure if you have to put the extension )

do not set newRaster to Nothing when you declare it, I don't know what it leads you to.


Jan 28, 2013 at 11:23 AM

It worked!!!

I declare newRaster as iRaster and then i make the call

newRaster = DotSpatial.Analysis.VectorToRaster.ToRaster(feats, cellSize, "Ide", "Selected.bgd", "", opt, Nothing)

Thank you so much Oscar

Sorry for waisting your time.



Jan 28, 2013 at 12:19 PM

Happy to hear that

Don't worry for my time, others helped me as I helped you.