SharpMap官方教程.docx
《SharpMap官方教程.docx》由会员分享,可在线阅读,更多相关《SharpMap官方教程.docx(29页珍藏版)》请在冰点文库上搜索。
![SharpMap官方教程.docx](https://file1.bingdoc.com/fileroot1/2023-5/5/f938bf19-e96c-4c47-bc6a-9f7f45044c78/f938bf19-e96c-4c47-bc6a-9f7f45044c781.gif)
SharpMap官方教程
HowtogetSharpMapsourcecode
V0.9/1.0
Youhavetwochoices:
1.DownloadfromCodePlex
1.Clickonthe"SourceCode"tab
2.Click"Download"inthe"LatestVersion"frame.
3.Acceptthelicenseagreementandpickaplacetosaveazip-fileofthecurrentversion,unpack.
4.Opensolutionandcompile.
2.Usesubversionclient(TortoiseSVN)
1.GetyouselfacopyofTortoiseSVNandinstall.
2.OpenExplorer,chooseplacetosaveyourcopyofSharpMap,andright-clickthemouse.
3.Fromthecontext-menuchoose"checkout"andfillinthefollowingurl:
*)andmaybechangethedestinationpath.Usernameandpasswordarethesameasonthecodeplexsite.
4.Opensolutionfileandcompile.
*)ThereareseveralbranchesforSharpMap
URL
Description
IntegrationofNetTopologySuitev1.7
Heavilyextendedv0.9.5version
PartialcloneofTrunkforaddingGeoJSONsupport(incorporatedinTrunk,maybedeletedsoon?
)
anexperimentalbranchfeaturingasyncrenderingandafewotherexperiments.Avoid
(
Iscurrentlybeeingdevelopedon
(
DONOTUSE,seebelow
BugFixbranchofuserBenjii
IntegrationofNTSv1.12(workinprogress)
V2.0
Thisversionisbeingdevelopedon
Latestprojectupdates
1.GetyouselfacopyofTortoiseSVNandinstall.
2.OpenExplorer,chooseplacetosaveyourcopyofSharpMap,andright-clickthemouse.
3.Fromthecontext-menuchoose"checkout",fillinthefollowingurl:
**)andmaybechangethedestinationpath.Leaveusernameandpasswordblank.
4.Opensolutionfileandcompile.
**)ThereareseveralbranchesforSharpMap
URL
Description
Gdal-,Ogr-andBrutileProvider
LasteditedDec202011at6:
20PM by FObermaier,version10
HowdoIcreateamap?
Initializethemap,addlayers,setupstyleandrenderit:
SharpMap.MapmyMap=newSharpMap.Map(newSize(400,300));
myMap.Size=newSystem.Drawing.Size(300,200);//Setoutputsize
myMap.MinimumZoom=100;//Minimumzoomallowed
myMap.BackgroundColor=Color.White;//Setbackground
myMap.Center=newSharpMap.Geometry.Point(725000,6180000);//Setcenterofmap
//AddPostGISlayer:
SharpMap.Layers.VectorLayermyLayer=newSharpMap.Layers.VectorLayer("Mylayer");
stringConnStr="Server=127.0.0.1;Port=5432;UserId=postgres;Password=password;Database=myGisDb;";
myLayer.DataSource=newSharpMap.Data.Providers.PostGIS(ConnStr,"myTable","the_geom",32632);
myLayer.MaxVisible=40000;
myMap.Layers.Add(myLayer);
//Renderthemap
////1stpossibility:
Setzoomlevel
//myMap.Zoom=1200;//Setzoomlevel
//2nd.possibility:
Zoomtoextents
myMap.ZoomToExtents();
System.Drawing.ImageimgMap=myMap.GetMap();
Note:
SharpMap.Data.Providers.PostgisismemberoftheSharpMap.Extensionsproject.
LasteditedMar92010at6:
24AM by FObermaier,version6
Transformingbetweenimagecoordinatesandworldcoordinates
SharpMaphasbuilt-infunctionstohelpyoutransformbetweenimageandworldcoordinates.
Whentheuserclicksthemapatagivenpixel(x,y),youcantranslatethisworldcoordinatesusingtheImageToWorldmethod:
SharpMap.Geometries.Pointp=myMap.ImageToWorld(newSystem.Drawing.PointF(x,y));
Pleasenotethatthevalueiscalculatedbasedonthecurrentzoomandcenter-valuesofthemyMapMapobject.
Ifyouwanttodotheoppositetransformation,useWorldToImage:
System.Drawing.PointFp=myMap.WorldToImage(newSharpMap.Geometries.Point(34.225,175.432));
LasteditedAug242007at9:
24AM by rstuven,version6
Creatingaclickablemap
System.Windows.Forms
1.InsertaPictureBoxforWindows.Forms.
2.Settheimagetoyourmap-instance.Ex.:
imgMap.Image=myMap.GetMap();
3.SetupaMouseClick-eventforyourPictureBox.
4.CreateaMouseClick-eventhandler,similartotheonebelow.
protectedvoidimgMap_MouseClick(objectsender,MouseClickEventArgse)
{
//Centerthemapontheclick-point
myMap.Center=myMap.ImageToWorld(newSystem.Drawing.PointF(e.X,e.Y),myMap);
//Zoomin2x
myMap.Zoom*=0.8;
//Callfunctionthatrendersthemapandreturnsittotheclient
myImage.Image=myMap.GetMap();
}
Thereareready-to-useUserControls(MapImage/MapBox)intheSharpMap.UIproject.Inadditiontozoomingfunctionalitytheyoffertoolsforqueryingandpanning.
ASP.NET
1.InsertanImageButton>forASP.NETwebforms.
2.Settheimagetoyourmap-instance.Ex.:
imgMap.Image=myMap.GetMap();
3.Setupaclick-eventforyourImageButton.
4.Createaclick-eventhandler,similartotheonebelow.
protectedvoidimgMap_Click(objectsender,ImageClickEventArgse)
{
//Centerthemapontheclick-point
myMap.Center=myMap.ImageToWorld(newSystem.Drawing.PointF(e.X,e.Y),myMap);
//Zoomin2x
myMap.Zoom*=0.8;
//Callfunctionthatrendersthemapandreturnsittotheclient
myImage.Image=myMap.GetMap();
}
Note:
Youneedtostorethezoomandcentervaluesbetweenrequests.ThiscanbedoneusingeitherViewStateorSessionvariables.Theseshouldberestoredonapostback,andstoredwhentheyarechanged.
LasteditedSep12010at6:
19PM by FObermaier,version11
HowdoIaddaWMSlayer?
TheWMSLayersupportiscurrentlyprettybasic.Youwillhavetodeciphertheservercapabilitiesyourself,andspecifythenessesarylayersandotherpropertiesintheresourceproperty.
Here'sanexampleofhowtoaddtheDemisWMSserver:
stringwmsUrl="http:
//www2.demis.nl/mapserver/request.asp";
SharpMap.Layers.WmsLayerlayWms=newSharpMap.Layers.WmsLayer("DemisMap",wmsUrl);
layWms.SpatialReferenceSystem="EPSG:
4326";//Setspatialreferencetouseforthisservice
//Addlayers(browsetheRootLayer/ChildLayerscollectiontofindavailable/validlayernames)
layWms.AddLayer("Bathymetry");
layWms.AddLayer("Countries");
layWms.AddLayer("Rivers");
layWms.AddLayer("Oceanfeatures");
//Setimageformattorequestfromserver
//SeelayWms.OutputFormatsarrayforformatssupportedbythisWMS
layWms.SetImageFormat("image/png");
//SkiprenderingtheWMSMapiftheservercouldn'tberequested
//Ifsettofalsesuchaneventwouldcrashtheapp.
layWms.ContinueOnError=true;
layWms.TimeOut=5000;//Settimeoutto5seconds
myMap.Layers.Add(layWms);//Addlayertoyourmap'slayercollection
Youcangetanexampleofhowanimagerequestwouldlooklike,byusingtheGetRequestUrlmethod.ThisisalsogoodfordebuggingtheWMSserver,sincetheserveroftenreturnsanerrormessageoninvalidrequests.
stringimageUrl=layWms.GetRequestUrl(newBoundingBox(0,0,360,180),newSize(600,300));
LasteditedAug92006at5:
46AM by SharpGIS,version5
HowdoIaddashapefilelayer?
UsetheShapeFileProvider:
SharpMap.Layers.VectorLayerlayWorld=newSharpMap.Layers.VectorLayer("World");
//Specifingtruewillsavethespatialindextoafilewhichwillmakeitloadquickerthenexttime
layWorld.DataSource=newSharpMap.Providers.ShapeFile(@"C:
\Data\Countries.shp",true);
myMap.Layers.Add(layWorld);
LasteditedAug42006at12:
55AM by SharpGIS,version6
HowtoaddaPostGISdatasourcetotheMap
ThePostGISproviderisusedforrenderinggeometrydatafromanPostGreSQLdatabase.YouwillneedtoinstallthePostGISextensioninPostGreSQLtouseitwithSharpMap.
Togetproperperformance,makesureyouhaveappliedaspatialindextothegeometrycolumn.
stringconnStr="Server=127.0.0.1;Port=5432;UserId=postgres;Password=pass;Database=myMapDB";//Connectionstring
stringtablename="Roads";//Nameoftableindatabase
stringidColumn="gid";//NameofobjectIDcolumn-MUSTbeinteger!
SharpMap.MapmyMap=newSharpMap.Map(newSystem.Drawing.Size(500,250));//Initializemapobject
SharpMap.Layers.VectorLayerlayRoads=newSharpMap.Layers.VectorLayer("Roads");//Createlayer
layRoads.DataSource=newSharpMap.Providers.OleDbPoint(connStr,tablename,gid);//SetthedatasourcetothePostGreSQLtable
myMap.Layers.Add(layRoads);//Addlayertomap
myMap.ZoomToExtents();//Zoomtoextents
Imageimg=myMap.GetMap();//Rendermap
YoucanfindmorehelpatthePostGISwebsiteonhowtoloadmapdataintothePostGreSQLdatabase,andhowtoinstallPostGISproperlyonyoudatabase.
Note:
InearlierversionsPostGISwasincludedinSharpMap,butisnowaseperateextensionthatneedstobeaddedtoSharpMap.SeePostGIS
LasteditedAug42006at1:
16AM by SharpGIS,version3
ThisexampleisderiviedfromtheAddaPostGISlayerexample.
stringtablename="Roads";//Nameoftableindatabase
stringidColumn="gid";//NameofobjectIDcolumn-MUSTbeinteger!
SharpMap.MapmyMap=newSharpMap.Map(newSystem.Drawing.Size(500,250));//Initializemapobject
SharpMap.Layers.VectorLayerlayRoads=newSharpMap.Layers.VectorLayer("Roads");//Createlayer
layRoads.DataSource=newSharpMap.Providers.OleDbPoint(connStr,tablename,gid);//SetthedatasourcetothePostGreSQLtable
//Setuparoadlabellayer
SharpMap.Layers.LabelLayerlayRoadLabel=newSharpMap.Layers.LabelLayer("Roadlabels");
layRoadLabel.DataSource=layRoads.DataSource;
layRoadLabel.Enabled=true;
layRoadLabel.LabelColumn="RoadOfNa