DotSpatialTutorial3.docx
- 文档编号:27392740
- 上传时间:2023-06-30
- 格式:DOCX
- 页数:17
- 大小:1.55MB
DotSpatialTutorial3.docx
《DotSpatialTutorial3.docx》由会员分享,可在线阅读,更多相关《DotSpatialTutorial3.docx(17页珍藏版)》请在冰豆网上搜索。
DotSpatialTutorial3
RasterDataOperationsinDotSpatial
Tutorial
(2)
Purposeofthistutorial:
BecomefamiliarwiththefollowingrasteroperationsinDotSpatial:
1.LoadingdifferentformatsofrasterdatainDotSpatial.
2.Implementingthehillshade.
3.Changethecoloroftherasterdata.
4.Multiplytherasterdata.
5.Reclassifytherasterdata.
6.Getthemouseclickedpointvaluesontherasterdatalayer.
Thistutorialhas5importantsteps.
Step1:
DownloadtheDotSpatialclasslibrary
Step2:
AddtheDotSpatialreferenceandchangethecompileoption.
Step3:
AddtheDotSpatialControlsintotheVisualStudioToolbox.
Step4:
CopytheDataExtensionsfoldertothedebugsfolderofthecurrentproject
Step5:
DesigntheGUI.(GUI-GraphicalUserInterface)
Step6:
Writethecodeforimplementingthemapoperations.
Step1:
DownloadtheDotSpatialclasslibrary
ThisstepisthesameasTutorial#1step1.
Step2:
AddtheDotSpatialreferenceandchangethecompileoption.
1.1)Addingthereferences.
DotSpatial.Data.Forms.dll,DotSpatial.Symbology.dll,DotSpatial.Controls.dll,DotSpatial.Projections.dll,DotSpatial.Data.dll,DotSpatial.Topology.dll
Fig.1Requiredreferences.
2.2)Changethecompileoption.
Changethecompileoptionfrom.NetFramework4ClientProfileto.NetFramework4.
ThisstepisthesameastheTutorial#1step2.2.
Step3:
AddtheDotSpatialControlsintotheVisualStudioToolbox.
ThisstepisthesameastheTutorial#1step3.
Step4:
CopytheDataExtensionfolderfromyourdownloadedfoldertoyourcurrentprojectbin/debugfolder.The.dllsfromthisfolderisnecessaryforGDALdataprovideraccess.
Fig.2DataExtensionsfolderfromDotSpatialdownloadedunzipfolder
Step5:
DesigntheGUI
DesigntheGUIasfollows:
Fig.3FinalGUI
Interfacedesignconsiderations.
1.Addthreepanelcontrols.Panelcontrol'spropertiesshouldbeasfollows:
Properties
Panel1
Panel2
Panel3
Name
pnlOperations
pnlLegend
pnlMap
Dock
Top
Left
Fill
2.Addfivebuttons.Buttonpropertiesshouldbeasfollows:
Properties
Button1
Button2
Button3
Button4
Button5
Name
btnLoadRaster
btnHillshade
btnChangeColor
btnMultiplyRaster
btnReclassify
Text
&LoadRaster
&Hillshade
Change&Color
&MultiplyRaster
&ReclassifyRaster
3.Addtwolabelcontrols.Labels'propertiesshouldbeasfollows:
Properties
Label1
Label2
Name
lblElevation
lblRasterValue
Text
Elevation
Row:
Column:
Value:
4.Addacheckboxanditshouldhavethefollowingproperties.
Name:
chbRasterValueText:
Rastervalueatclickedpoint
5.Addatextboxanditshouldhavethefollowingfeatures.
Name:
txtElevationText:
3000
6.Draga“Legend”controlfromtheDotSpatialtabundertoolboxanddropitonpnlLegend.Legendpropertiesshouldbeasfollows:
Name:
Legend1Dock:
Fill
7.Draga“Map”controlfromtheDotSpatialtabundertoolboxanddropitonpnlMap.Mappropertiesshouldbeasfollows:
Name:
Map1Dock:
FillLegend:
Legend1
8.Dragan"AppManager"controlfromDotSpatialtabundertoolboxanddropitontheform.
Note:
Thiscontrolisnecessaryforloadingdifferentformatsofrasterdata.
Fig.4AppManager
9.SetthepropertiesofAppManager1asfollows:
Map:
Map1Legend:
Legend1
Step:
6Loadingthedifferentformatsofrasterdata.
6.1)Importthefollowingnamespacesinthecodingwindow.
//Requirednamespaces
usingDotSpatial.Symbology;
usingDotSpatial.Controls;
usingDotSpatial.Data;
usingDotSpatial.Topology;
6.2)btnLoadRasterclickeventshouldbeasfollows:
privatevoidbtnLoadRaster_Click(objectsender,EventArgse)
{
//AddRasterLayer()methodisusedtoloadtherasterlayersonthemap
map1.AddRasterLayer();
map1.ZoomToMaxExtent();
}
Implementingthehillshade.
btnHillshade_Clickeventshouldhavethefollowingcode.
privatevoidbtnHillshade_Click(objectsender,EventArgse)
{
if(map1.Layers.Count>0)
{
//IMapRasterLayerlayer=(IMapRasterLayer)map1.Layers[0];
IMapRasterLayerlayer=map1.Layers[0]asIMapRasterLayer;
if(layer==null)
{
MessageBox.Show("Pleaseselectarasterlayer");
return;
}
layer.Symbolizer.ShadedRelief.ElevationFactor=1;
layer.Symbolizer.ShadedRelief.IsUsed=true;
layer.WriteBitmap();
}
else
{
MessageBox.Show("Pleaseaddalayertothemap.");
}
}
Changethecolor.
btnChangeColorclickeventshouldhasthefollowingevent.
privatevoidbtnChangeColor_Click(objectsender,EventArgse)
{
if(map1.Layers.Count>0)
{
//changethecolorofraster
//typecastthefirstlayertoMapRasterLayer
IMapRasterLayerlayer=map1.Layers[0]asIMapRasterLayer;
if(layer==null)
{
MessageBox.Show("Pleaseaddarasterlayer.");
return;
}
//setthecolorscheme
//createaninstanceforacolorscheme
ColorSchemescheme=newColorScheme();
//createanewcategory
ColorCategorycategory1=newColorCategory(2500,3000,Color.Red,Color.Yellow);
category1.LegendText="Elevation2500-3000";
//addthecategorytothecolorscheme
scheme.AddCategory(category1);
//createanothercategory
ColorCategorycategory2=newColorCategory(1000,2500,Color.Blue,Color.Green);
category2.LegendText="Elevation1000-2500";
scheme.AddCategory(category2);
//assignnewcolorscheme
layer.Symbolizer.Scheme=scheme;
//refreshthelayerdisplayinthemap
layer.WriteBitmap();
}
else
{
MessageBox.Show("Pleaseaddalayertothemap.");
}
}
Multiplytheraster.
btnMultiplyRastershouldhasthefollowingevent.
privatevoidbtnMultiplyRaster_Click(objectsender,EventArgse)
{
if(map1.Layers.Count>0)
{
IMapRasterLayerlayer=map1.Layers[0]asIMapRasterLayer;
if(layer==null)
{
MessageBox.Show("Pleaseselectarasterlayer");
}
IRasterdemRaster=layer.DataSet;
string[]rasterOptions=newstring[1];
IRasternewRaster=Raster.CreateRaster("multiply.bgd",null,demRaster.NumColumns,demRaster.NumRows,1,demRaster.DataType,rasterOptions);
//Boundsspecifythecellsizeandthecoordinatesofrastercorner
newRaster.Bounds=demRaster.Bounds.Copy();
newRaster.NoDataValue=demRaster.NoDataValue;
newRaster.Projection=demRaster.Projection;
for(inti=0;i<=demRaster.NumRows-1;i++)
{
for(intj=0;j<=demRaster.NumColumns-1;j++)
{
if(demRaster.Value[i,j]!
=demRaster.NoDataValue)
{
newRaster.Value[i,j]=demRaster.Value[i,j]*2;
}
}
}
//savethenewrastertothefile
newRaster.Save();
//addthenewrastertothemap
map1.Layers.Add(newRaster);
}
else
{
MessageBox.Show("Pleaseaddalayertothemap.");
}
}
Reclassifytheraster.
btnReclassifyshouldhasthefollowingevent.
privatevoidbtnReclassify_Click(objectsender,EventArgse)
{
//typecasttheselectedlayertoIMapRasterLayer
IMapRasterLayerlayer=map1.Layers.SelectedLayerasIMapRasterLayer;
if(layer==null)
{
MessageBox.Show("Pleaseselectarasterlayer.");
}
else
{
//gettherasterdataset
IRasterdemRaster=layer.DataSet;
//createanewemptyrasterwithsamedimensionasoriginalraster
string[]rasterOptions=newstring[1];
IRasternewRaster=Raster.CreateRaster("reclassify.bgd",null,demRaster.NumColumns,demRaster.NumRows,1,demRaster.DataType,rasterOptions);
newRaster.Bounds=demRaster.Bounds.Copy();
newRaster.NoDataValue=demRaster.NoDataValue;
newRaster.Projection=demRaster.Projection;
//reclassifyraster.
//values>=specifiedvaluewillhavenewvalue1
//values doubleoldValue=0; //getthespecifiedvaluefromthetextbox doublespecifiedValue=Convert.ToDouble(txtElevation.Text); for(inti=0;i<=demRaster.NumRows-1;i++) { for(intj=0;j<=demRaster.NumColumns-1;j++) { //getthevalueoforiginalraster oldValue=demRaster.Value[i,j]; if(oldValue>=specifiedValue) { newRaster.Value[i,j]=1; } else { newRaster.Value[i,j]=0; } } } newRaster.Save(); map1.Layers.Add(newRaster); } } Getthemouseclickedpointvaluesontherasterdatalayer. SelectthechbRasterValue's"CheckedChanged"event. fig.5ChbRasterValuecheckbox'scheckedchangedevent. chbRasterValue_CheckedChangedeventshouldhasthefollowingevents. privatevoidchbRasterValue_CheckedChanged(objectsender,EventArgse) { if(chbRasterValue.Checked) { IMapRasterLayerrasterLayer=map1.Layers.SelectedLayerasIMapRasterLayer; if((rasterLayer! =null)) { //setthemapcursortocross map1.Cursor=Cursors.Cross; } else { //ifnorasterlayerisselected,uncheckthecheckbox MessageBox.Show("Pleaseselectarasterlayer."); chbRasterValue.Checked=false; } } else { //changemapcursorbacktoarrow map1.Cursor=Cursors.Arrow; } } SelecttheMap1'smouseupevent. fig.6Map1_MouseUpevent map1_MouseUpeventshouldhavethefollowingcode. privatevoidmap1_MouseUp(objectsender,MouseEventArgse) { if(chbRasterValue.Checked) { //getthelayerselectedinthelegend IMapRasterLayerrasterLayer=map1.Layers.SelectedLayerasIMapRasterLayer; if((rasterLayer! =null)) { //gettherasterdataobject IRasterraster=rasterLayer.DataSet; //convertmousepositiontomapcoordinate Coordinatecoord=map1.PixelToProj(e.Location); //convertmapcoordinatetorasterrowandcolumn RcIndexrc=raster.Bounds.ProjToCell(coord); introw=rc.Row; intcolumn=rc.Column; //checkifclickedpointisinsideofraster if((column>0&column { //gettherastervalueatrowandcolumn doublevalue=raster.Value[row,column]; //showtherow,columnandvalueinthelabel lblRasterValue.Text
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DotSpatialTutorial3