Showing posts with label ArcObject Example. Show all posts
Showing posts with label ArcObject Example. Show all posts

Sunday, May 24, 2009

ArcEditor or ArcGIS Licence Example code

An ArcGIS Engine-based application must initialize certain environment variables and check out a license before it can execute.
First, initialize environment variables and acquire an Engine license.
EngineInitializer.initializeEngine();
AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);If you are working with the ArcGIS controls (visual JavaBeans found in the com.esri.arcgis.beans.xxxx packages), then the initialization code looks like this:
EngineInitializer. initializeVisualBeans();
AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
If your application requires the use of an extension product, such as Spatial Analyst or 3D Analyst, then you need to check these licenses as well. This code cannot precede the initial license checkout.
aoInit.initialize(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);
aoInit.initialize(esriLicenseExtensionCode.esriLicenseExtensionCode3DAnalyst);

Open a SDE Connection with Database

This example uses ArcObjects to connect directly to ArcSDE and get data sets rather than using the MapServer object to get the ArcSDE layers in a Map.
Create a WorkspaceFactory object within the Server's context.


IWorkspaceFactory pWorkspaceFactory = new IWorkspaceFactoryProxy(context.createObject("esriDataSourcesGDB.SdeWorkspaceFactory"));
Create an instance of a PropertySet for an Oracle ArcSDE connection. The PropertySet acts as an array of keyed values that ArcSDE will use to collect the connection values from:
PropertySet propSet = new PropertySet(context.createObject("esrisystem.PropertySet"));
propSet.setProperty("SERVER", "tel");
propSet.setProperty("INSTANCE", "5051");
propSet.setProperty("DATABASE", "");
propSet.setProperty("USER", "map");
propSet.setProperty("PASSWORD", "map");
propSet.setProperty("VERSION", "SDE.DEFAULT");



Open the ArcSDE workspace and get a handle to it through the WorkspaceFactory, passing in the PropertySet:
IWorkspace ws = pWorkspaceFactory.open(propSet,0);
You now have a connection to the database through a Workspace object (“ws”). In this example, the feature datasets are listed out:
IEnumDataset dsenum = ws.getDatasets(esriDatasetType.esriDTFeatureDataset);
IDataset ds = dsenum.next();
while(ds != null){
System.out.println(ds.getName());
ds = dsenum.next();
}

Thursday, May 21, 2009

WebServices in GIS


Proximity Web Service overview
You can use the Proximity Web Service to find all points of interest (POI) locations within a distance you define for a specified point or line (for example, find all POIs within five miles of x,y) or determine the nearest specified number of POI locations to a specified point or line (such as find nearest three POIs to x,y).
The Proximity Web Service offers the following capabilities:
Find nearest features.
Find features within a specified radius.
Return list and related information of found features.
Limit searches based on user-specified criteria.



https://arcweb.esri.com/services/v2/Proximity.wsdl
Note: HTTPS is available if you need added security to call ArcWeb Services. The Authentication Web Service and other ArcWeb Services must be accessed with the same IP address for the authentication token to be valid.


See live example




IProximity Interface Example code

Private Sub GetNearestPoint()
Dim pPoint As IPoint
Dim pProximity As IProximityOperator
Dim pGeom As IGeometry
Dim pFeature As IFeature, pTestFeature As IFeature, pPointFeature As IFeature
Dim tempDist As Double, searchDist As Double, testDistance As Double, pointTestDistance As Double
Dim pFeatureClass As IFeatureClass
Dim pQueryFilter As IQueryFilter
Dim pFeatureCursor As IFeatureCursor
Dim iCount As Integer
Dim iDescription As Long, iOID As Long
Dim sClosest As String

On Error GoTo ErrorHandler
sClosest = "Nothing Found"
pointTestDistance = -1
testDistance = -1
searchDist = 500
iCount = 0

Set pFeatureClass = pFeatureWorkspace.OpenFeatureClass("ALL_CITIES")
Set pQueryFilter = New QueryFilter
'Get the search point
pQueryFilter.WhereClause = "WHERE OBJECTID = 23746"
Set pFeatureCursor = pFeatureClass.Search(pQueryFilter, False)
iDescription = pFeatureCursor.FindField("LOC_DESC")
iOID = pFeatureCursor.FindField("OBJECTID")

Set pTestFeature = pFeatureCursor.NextFeature
Debug.Print "?" & pTestFeature.Value(iOID) & " " & pTestFeature.Value(iDescription)
Set pPoint = pTestFeature.Shape
Set pProximity = pPoint

'Get the search data
pQueryFilter.WhereClause = "WHERE CNTRY = 'XX' and OBJECTID <> 23746"
Set pFeatureCursor = Nothing
Set pTestFeature = Nothing
Set pFeatureCursor = pFeatureClass.Search(pQueryFilter, False)
Set pTestFeature = pFeatureCursor.NextFeature

Debug.Print "START****" & Time
Do While (Not pTestFeature Is Nothing)
Set pGeom = pTestFeature.Shape
tempDist = pProximity.ReturnDistance(pGeom)
If (tempDist < searchDist) Then
If (pointTestDistance < 0) Then pointTestDistance = tempDist + 1
If (tempDist < pointTestDistance Or pointTestDistance = -1) Then
sClosest = pTestFeature.Value(iOID) & " " & pTestFeature.Value(iDescription) & " " & " [" & tempDist & "]"
Debug.Print sClosest
pointTestDistance = tempDist
Set pPointFeature = pTestFeature
End If
End If
iCount = iCount + 1
Set pTestFeature = pFeatureCursor.NextFeature
Loop
Debug.Print "END****" & Time
Debug.Print "Records parsed: " & iCount
Debug.Print "Closest Point: " & sClosest
Debug.Print "===================================================="
Exit Sub
ErrorHandler:
MsgBox "An unexpected error has occured in GetNearestPoint." & vbCr & vbCr & _
"Details : " & Err.Description, vbExclamation + vbOKOnly, "Error"
End Sub

Monday, May 18, 2009

Spatial Filter Example code


Count features within an area



DescriptionThis sample uses a spatial query filter to count the number of features within an area. First, click the tool button (you create) on the toolbar, then click and drag a rectangle around the features you want to have counted. The program retrieves the cursor's locations based on a filter, then loops through a counting procedure of all the features. The number of points, lines, and areas are totaled and reported to the user on a message box. In this sample, you will be adding a control tool and writing the code for it.




Start ArcMap.
Open an existing map document (.mxd) or add layers to the empty (Untitled) map document.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIToolControl as the UIControl Type.
Click Create.
Click and drag the new Project.UIToolControl1 in the Commands list and drop it on any toolbar.
Click Close.
Right-click the newly placed control and click View Source. This opens the Visual Basic Editor.
In the ThisDocument (Code) window, click the Procedure Box drop-down arrow (the one on the right of the window) and click MouseDown. This adds the wrapper code for the procedure you are creating.
Copy and paste the following code between the two wrapper code lines (between

---------------------------------------------------------------------------------------------

Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long) and End Sub): Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pEnv As IEnvelope
Dim pRubber As IRubberBand
Set pRubber = New RubberEnvelope
Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.FocusMap
Set pEnv = pRubber.TrackNew(pActiveView.ScreenDisplay, Nothing)
Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = pEnv
pSpatialFilter.SpatialRel = esriSpatialRelIntersects
Dim lPoints As Long, lPolylines As Long, lPolygons As Long
Dim pLayer As IFeatureLayer
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim i As Long
For i = 0 To pMxDoc.FocusMap.LayerCount - 1
If (TypeOf pMxDoc.FocusMap.Layer(i) Is IGeoFeatureLayer) Then
Set pLayer = pMxDoc.FocusMap.Layer(i)
pSpatialFilter.GeometryField = pLayer.FeatureClass.ShapeFieldName
Set pFeatureCursor = pLayer.Search(pSpatialFilter, True)
Set pFeature = pFeatureCursor.NextFeature
Do Until (pFeature Is Nothing)
Select Case pFeature.Shape.GeometryType
Case esriGeometryPoint
lPoints = lPoints + 1
Case esriGeometryPolyline
lPolylines = lPolylines + 1
Case esriGeometryPolygon
lPolygons = lPolygons + 1
End Select
Set pFeature = pFeatureCursor.NextFeature
Loop
End If
Next i
MsgBox "Features Found:" & vbCrLf & lPoints & " Points " & vbCrLf & _
lPolylines & " Polylines " & vbCrLf & lPolygons & " Polygons "

---------------------------------------------------------------------------------------------
Close the Visual Basic Editor.
Click the Tool button in ArcMap, then click and drag to select an area in which to count the features.

Display a mouse location in decimal degrees


This sample allows the user to view the mouse location in decimal degrees when the user clicks with the mouse.In this sample, you will be adding a new tool to the interface and writing the code for the tool.


Start ArcMap.
Open an existing map document (.mxd) or add layers to the empty (Untitled) map document. The data added to the map must be projected.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIToolControl as the UIControl Type.
Click Create and Edit.
Copy and paste the following code after all other code in the ThisDocument (Code) window.Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
Dim pMxDoc As IMxDocument
Dim pPoint As IPoint
Dim pClone As IClone
Dim pGeometry As IGeometry
Dim pSpatialRefFactory As ISpatialReferenceFactory
Dim pSpatialRef As ISpatialReference
Dim pGeographicCoordSys As IGeographicCoordinateSystem
'Get the point where the user clicked
Set pMxDoc = Application.Document
If pMxDoc.CurrentLocation.IsEmpty Then Exit Sub
'Clone the point because we don't want to alter
'the actual document's current location point
Set pClone = pMxDoc.CurrentLocation
Set pPoint = pClone.Clone
Set pGeometry = pPoint 'QI
'Create a new geographic coordinate system to use in the conversion
Set pSpatialRefFactory = New SpatialReferenceEnvironment
Set pGeographicCoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCS_NAD1983)
Set pSpatialRef = pGeographicCoordSys 'QI
pSpatialRef.SetFalseOriginAndUnits -180, -90, 1000000
pGeometry.Project pSpatialRef
MsgBox pPoint.x & ", " & pPoint.y, , "Decimal Degrees"
End Sub
Close or minimize the VBA window.
In ArcMap, click Tools and click Customize.
Click the Commands Tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new control was saved.
Scroll through the Categories list and click [UIControls].
Click the Project.UIToolControl1 in the Commands list. Click and drag this button and drop it onto any toolbar.
Click Close.
Click the tool and click anywhere on the map.

Add a Shape file Programatically


Dear all

find this steps to understand the arcobject code for Adding shape file programatically

This sample, which can easily be changed to support different data types, opens a shapefile on your local disk and adds the contents to the map as a feature layer. In this sample, you will be adding a control button and writing the code for it.


Start ArcMap.
Open an existing map document(.mxd) or add layers to the empty (Untitled) map document.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIButtonControl as the UIControl Type.
Click Create.
Click and drag the new Project.UIButtonControl1 in the Commands list and drop it on any toolbar.
Click Close.
Right-click the newly placed control and click View Source. This opens the Visual Basic Editor.
In the ThisDocument (Code) window, click the Procedure Box drop-down arrow (the one on the right of the window) and choose Click. This adds the wrapper code for the procedure you are creating.
Copy and paste the following code between the two wrapper code lines (between Private Sub UIButtonControl1_Click() and End Sub). Dim pWorkspaceFactory As IWorkspaceFactory
Set pWorkspaceFactory = New ShapefileWorkspaceFactory
Dim pWorkSpace As IFeatureWorkspace
'Change C:\Source to the source location of the shapefile you wish to add
Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\GISQuery\", 0)
Dim pClass As IFeatureClass
'Change USStates to the name of the shapefile you wish to add
Set pClass = pWorkSpace.OpenFeatureClass("GISQuery")
Dim pLayer As IFeatureLayer
Set pLayer = New FeatureLayer
Set pLayer.FeatureClass = pClass
pLayer.Name = pClass.AliasName
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
pMxDoc.AddLayer pLayer
pMxDoc.ActiveView.PartialRefresh esriViewGeography, pLayer, Nothing
Go to line 5, Set pWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Source", 0), and change C:\Source to the source location of the shapefile you want to add.
Go to line 8, Set pClass = pWorkSpace.OpenFeatureClass("GISQuery"), and change GISQuery to the name of the shapefile you want to add.
Close the Visual Basic Editor.
Click the new button in ArcMap to add the feature class to the map.