POST
|
Thanks for the help. I am only a beginner modifying someone's code so I don't seem to able to do it. My GetFeatureClass method gets a named feature class from a layer in the active map document. Originally the code gets a shapefile that has been added to the map but what I want it to do is get a featureclass that has been added to the map. Public Function GetFeatureClass(ByVal layername1 As String) As IFeatureClass Dim mxDocument As IMxDocument mxDocument = m_application.Document Dim map As IMap = mxDocument.FocusMap Dim check As Boolean = False Dim layerCounter As Integer Dim FeatureClass1 As IFeatureClass = Nothing Dim featureLayer As IFeatureLayer Dim currentLayer As ILayer For layerCounter = 0 To map.LayerCount - 1 Step layerCounter + 1 currentLayer = map.Layer(layerCounter) If (TypeOf currentLayer Is IFeatureLayer) And (currentLayer.Name = layername1) Then featureLayer = currentLayer FeatureClass1 = featureLayer.FeatureClass check = True layerCounter = map.LayerCount End If Next If check = False Then MsgBox("cannot find InputStops layer") Return Nothing End If Return FeatureClass1 End Function I've had to start writing my thesis so haven't had as much time as I hoped to modify the code
... View more
08-20-2013
03:10 AM
|
0
|
0
|
10
|
POST
|
Could anyone offer any more help on this? Do I use a function to connect to the GDB like this one Public Shared Function FileGdbWorkspaceFromPath(ByVal FGDB_WORKSPACE As String) As IWorkspace Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory") Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory) Return workspaceFactory.OpenFromFile(FGDB_WORKSPACE, 0) End Function If so, how would I go about calling that to create the feature there?
... View more
08-16-2013
12:49 AM
|
0
|
0
|
10
|
POST
|
Apologies for my ignorance, I'm new to this. How do I know what feature class I used to call IFeatureClass::CreatFeature? And how do I open the feature class from the database I want? This is where I call CreateFeature, about half way down. Public Sub go() initlicence() 'MonitorThread = New Thread(AddressOf Monitor) 'MonitorThread.Priority = ThreadPriority.BelowNormal 'MonitorThread.Start() ' convertjunctionfile(0.002910881) Dim featureclass1 As IFeatureClass = GetFeatureClass("StopsInput") Dim featureclassCensus As IFeatureClass = GetFeatureClass("census_enumerator_areas") 'Dim featureclassPOW As IFeatureClass = GetFeatureClass("pow_area_point") Dim featureclassED As IFeatureClass = GetFeatureClass("electoral_divisions") Dim EDindex As Integer = -1 Dim censusindex As Integer = -1 Dim eastingsindex As Integer = -1 Dim northingsindex As Integer = -1 For j = 0 To header.Length - 1 Select Case header(j) Case "Residence_ED" EDindex = j Case "Residence_2006_Enumeration_Area" censusindex = j Case "Fuzz_East_250" eastingsindex = j Case "Fuzz_North_250" northingsindex = j End Select Next 'check headers have been read in corrrectly If (EDindex = -1 Or censusindex = -1 Or eastingsindex = -1 Or northingsindex = -1) Then MsgBox("Error reading in input file header") End If 'MsgBox("total header length: " + header.Length.ToString) 'MsgBox("EDindex" + EDindex.ToString) ' MsgBox("censusindex" + censusindex.ToString) ' MsgBox("eastingsindex" + eastingsindex.ToString) MsgBox("northingsindex" + northingsindex.ToString) Dim route1length As Integer Dim startOID As Integer = 0 Dim inputVals() As String Dim startpoint As ESRI.ArcGIS.Geometry.Point Dim endpoint As ESRI.ArcGIS.Geometry.Point = New ESRI.ArcGIS.Geometry.Point Dim route1() As Integer Dim routeClass As RouteClass = New RouteClass Dim crowflies As Double = Nothing routeClass.Init() Dim OutputDatabase1 As OutputDatabase = New OutputDatabase Dim numPrevProcessed As Integer = 0 If continue_from_prev_from_form = True Then OutputDatabase1.continueFromOld() numPrevProcessed = OutputDatabase1.getNumberProcessed() 'MsgBox("num prev processed: " + numPrevProcessed.ToString) 'This loop cycles past any previously processed details For m As Integer = 1 To numPrevProcessed TextFromFile1.Read() Next Else 'Initialise database using header information OutputDatabase1.init(header) MsgBox("OutputGeodatabase") End If 'initialise variables For Me.person_number = (numPrevProcessed + 1) To (number_to_process_fromform + numPrevProcessed + 1) 'updates global person number, for error reporting purposes person_no_global = person_number 'Clears any previous route stops from the screen DeleteRows(featureclass1) inputVals = TextFromFile1.Read() MsgBox(inputVals.ToString) 'This if statement exits the loop if the reader reaches the end of the file If inputVals Is Nothing Then TextFromFile1.errorwriter("End of input file. All entries processed", person_number) Exit For End If 'Conditions to check that input file info is all present and intact If (IsNumeric(inputVals(censusindex)) Or IsNumeric(inputVals(EDindex))) And IsNumeric(inputVals(eastingsindex)) And IsNumeric(inputVals(northingsindex)) Then MsgBox("1") If (Not IsNumeric(inputVals(censusindex))) Or inputVals(censusindex) = "*" Then startpoint = FindCentroid(featureclassED, inputVals(EDindex), False) MsgBox("2") Else startpoint = FindCentroid(featureclassCensus, inputVals(censusindex), True) MsgBox("startpoint: " + startpoint.ToString) MsgBox("3") 'If point can't be found from census area then use If startpoint Is Nothing Then startpoint = FindCentroid(featureclassED, inputVals(EDindex), False) MsgBox("4") End If End If 'initialise startOID and route startOID = Nothing route1 = Nothing route1length = 0 crowflies = Nothing Command1.resultDistance = 0 'condition in case startpoint can't be found If Not startpoint Is Nothing Then MsgBox("11") MsgBox("person: " + person_no_global.ToString) 'Dim endpoint As ESRI.ArcGIS.Geometry.Point = FindPOW(featureclassPOW, 400) endpoint.X = inputVals(eastingsindex) + (Rnd(person_number + 0.2) - 0.5) * 2 * 125 endpoint.Y = inputVals(northingsindex) + (Rnd(person_number + 0.3) - 0.5) * 2 * 125 CreateFeature("StopsInput", startpoint.X, startpoint.Y, featureclass1) CreateFeature("StopsInput", endpoint.X, endpoint.Y, featureclass1) 'selectall() 'Call routing functions MsgBox("6 " + startpoint.X.ToString) 'route1 = Router() route1 = routeClass.SolveRoute() MsgBox("7") If Not route1 Is Nothing Then MsgBox("Route Nothing") startOID = OutputDatabase1.WriteToRouteTableRowField(person_number, route1) route1length = route1.Length Else route1length = 0 MsgBox("route length") End If crowflies = crowFliesDistance(startpoint, endpoint) MsgBox("8") End If OutputDatabase1.WriteToMainTableRowField(person_number, startOID, inputVals, route1length, crowflies, resultDistance) MsgBox("9") Else OutputDatabase1.WriteToMainTableRowField(person_number, Nothing, inputVals, 0, 0, 0) MsgBox("10") TextFromFile1.errorwriter("Missing info (ED/census or POW co-ords for routing. Person no", person_number) End If 'updates progress bar every 10 people 'If person_number Mod 10 = 0 Then 'progressNum = CType(System.Math.Abs(((person_number - numPrevProcessed) * 100) / number_to_process_fromform), Integer) 'If progressNum <= 100 Then 'UpdateUI(progressNum) 'End If If person_number Mod 100 = 0 Then 'OPTIONAL GARBAGE COLLECTOR CALL 'Used to force system to clean out unused areas of ram every 100 cycles System.GC.Collect() End If 'End If 'progressbox1.ProgressBar1.Value = 49 + person_number * 50 'progressbox1.Refresh() 'routeprogress.Invoke(New UpdateUIDelegate() ), New Object() {50 + person_number * 50}) MsgBox(person_number) Next person_number 'routeClass.SaveLayerToDisk(routeClass.naLayerStore, System.Environment.CurrentDirectory + "\RouteGEN.lyr") 'routeprogress System.GC.Collect() 'errorwriter("Finished successfully", person_number) 'Adds Route layer to display 'Dim mxDocument As IMxDocument = GetMxDocumentFromArcMap(m_application) 'Dim activeView As IActiveView = mxDocument.ActiveView 'AddLayerFromFile(activeView, routeClass.SHAPE_WORKSPACE + "RouteGEN.lyr") 'ESRI License Initializer generated code. 'Do not make any call to ArcObjects after ShutDownApplication() OutputDatabase1 = Nothing Try 'progressbox1.Close() MonitorThread.Abort() 'progressbox1 = Nothing Catch ex As Exception End Try m_AOLicenseInitializer.ShutdownApplication() MsgBox("Routing done: " + (person_number - 1).ToString + " routes calculated in total. Finished at: " + TimeOfDay.ToLongTimeString()) End Sub
... View more
08-15-2013
09:44 AM
|
0
|
0
|
10
|
POST
|
I'm creating a feature and I want to store it to a specific geodatabase. At the moment I'm using feature.Store() but it's not storing it to where I want. How do I specify a location? I want to store it to C:\College Stuff\Dissertation\2009 stuff\Geo\Geo\Full_3D.gdb Public Sub CreateFeature(ByVal layername1 As String, ByVal x As Double, ByVal y As Double, ByVal FeatureClass1 As IFeatureClass)
Dim cPoint As IPoint = New ESRI.ArcGIS.Geometry.Point
' To individually assign a value to the coordinates, use cPoint.X = x and cPoint.Y = y.
cPoint.PutCoords(x, y)
' Ensure the feature class contains points.
If FeatureClass1.ShapeType <> esriGeometryType.esriGeometryPoint Then
Return
End If
' Build the feature.
Dim feature As IFeature = FeatureClass1.CreateFeature()
feature.Shape = cPoint
' Commit the new feature to the geodatabase.
feature.Store()
End Sub
... View more
08-15-2013
09:06 AM
|
0
|
6
|
301
|
POST
|
So the problem isn't with initialising, I've figured out. The code works past that, the problem seems to be when I call route1=routeclass.SolveRoute(), which leads me to believe that there is something wrong in the RouteClass. Anyone have any ideas? I tried the activator but I didn't change anything
... View more
08-15-2013
01:31 AM
|
0
|
0
|
4
|
POST
|
Do you mind me asking how I would use the Activator class? I'm quite new to this Also, do you know any reason why it worked a month ago but won't work now? I didn't change anything. I'm due to submit a project soon and this is a large part of it so I'm panicking a little as I have no idea how to fix it
... View more
08-13-2013
09:02 AM
|
0
|
0
|
4
|
POST
|
I have an add in tool that is basically compiling routes of people travelling to work. It is a large tool ( a lot of code) and it worked a month ago. When I came back to use it yesterday it suddenly doesn't work any more. I think I've narrowed it down to the RouteClass which was designed from the Route Layer sample here http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000041z000000 My code seems to crash when I initialize the routeclass. I'll post the section where I think it is going wrong. MsgBox("6 " + startpoint.X.ToString) routeClass.Init() route1 = routeClass.SolveRoute() If Not route1 Is Nothing Then startOID = OutputDatabase1.WriteToRouteTableRowField(person_number, route1) route1length = route1.Length MsgBox("99" + route1length.ToString) Else route1length = 0 End If crowflies = crowFliesDistance(startpoint, endpoint) MsgBox("22" + crowflies.ToString) End If End If This is only a small section of the code, the message boxes are for me to see where the code stops. The last message box that worked is MsgBox("6 " + startpoint.X.ToString) . Here is my RouteClass Public Class RouteClass Public Const SHAPE_WORKSPACE As String = "C:\Users\Paul\Downloads\Geo\" Private Const INPUT_STOPS_FC As String = "StopsInput" Private Const SHAPE_INPUT_NAME_FIELD As String = "StopsInput" Private Const NETWORK_DATASET As String = "DTO_Network_ND" 'Public naLayerStore As INALayer Dim workspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactoryClass() Dim featureWorkspace As IFeatureWorkspace = workspaceFactory.OpenFromFile(SHAPE_WORKSPACE, 0) Dim pUID As UID = New UIDClass() Dim networkDataset As INetworkDataset Dim inputStopsFClass As IFeatureClass Dim NALayer As INALayer Dim naContext As INAContext Dim stopsNAClass As INAClass Dim routesFC As IFeatureClass Dim naClassFieldMap As INAClassFieldMap Dim naLoader As INAClassLoader Dim blankstops As Object Dim messages As New GPMessagesClass() Dim naSolver As INASolver Public Sub Init() pUID.Value = "esriGeoDatabase.NetworkDatasetWorkspaceExtension" Dim workspaceExtensionManager As IWorkspaceExtensionManager = featureWorkspace Dim datasetContainer2 As IDatasetContainer2 = workspaceExtensionManager.FindExtension(pUID) networkDataset = datasetContainer2.DatasetByName(esriDatasetType.esriDTNetworkDataset, NETWORK_DATASET) End Sub 'Create the analysis layer, load the locations, solve the analysis, and write to disk Public Function SolveRoute() As Integer() ' Open the feature workspace, input feature class, and network dataset inputStopsFClass = featureWorkspace.OpenFeatureClass(INPUT_STOPS_FC) ' Create the Route NALayer NALayer = CreateRouteAnalysisLayer("Route", networkDataset) naContext = NALayer.Context stopsNAClass = naContext.NAClasses.ItemByName("Stops") routesFC = naContext.NAClasses.ItemByName("Routes") ' Load the Stops naClassFieldMap = New NAClassFieldMapClass() naClassFieldMap.MappedField("Name") = SHAPE_INPUT_NAME_FIELD naLoader = New NAClassLoaderClass() naLoader.Locator = naContext.Locator naLoader.NAClass = stopsNAClass naLoader.FieldMap = naClassFieldMap naLoader.Load(inputStopsFClass.Search(Nothing, True), New CancelTrackerClass(), 0, 0) 'New QueryFilterClass() ' Message all of the network analysis agents that the analysis context has changed 'Dim naContextEdit As INAContextEdit = naContext 'naContextEdit.ContextChanged() 'Solve messages = New GPMessagesClass() Try naSolver = naContext.Solver naSolver.Solve(naContext, messages, New CancelTrackerClass()) Catch e As Exception TextFromFile1.errorwriter("Solver exception: " + e.Message, MakeRoute.person_no_global) If Not messages.Description Is Nothing Then TextFromFile1.errorwriter("Solver error: " + messages.Description, MakeRoute.person_no_global) Else TextFromFile1.errorwriter("Solver error: ", MakeRoute.person_no_global) End If Return Nothing End Try 'Save the layer to disk (disabled to improve execution speed 'SaveLayerToDisk(NALayer, System.Environment.CurrentDirectory + "\RouteGEN.lyr") 'naLayerStore = naLayer 'AREA FOR EXTRACTING THE FEATURES TRAVERSED BY THE ROUTE Dim naTraversalResult As INATraversalResult = CType(naLayer.Context.Result, INATraversalResult) Dim naTraversalResultQuery As INATraversalResultQuery = CType(naTraversalResult, INATraversalResultQuery) Dim traversalFClass As IFeatureClass = naTraversalResultQuery.FeatureClass(esriNetworkElementType.esriNETEdge) Dim featureCursor1 As IFeatureCursor = traversalFClass.Search(Nothing, True) Dim feature As IFeature = featureCursor1.NextFeature() If feature Is Nothing Then MsgBox("no result found to be added to database") End If ' Loop through all the traversal result elements for that feature Dim routeNums() As Integer = {0} Dim i As Integer = 0 Dim totlength As Double = 0 Dim poly As IPolyline While Not feature Is Nothing poly = feature.Shape totlength += poly.Length 'This is the FID value of each feature ReDim Preserve routeNums(0 To i) routeNums(i) = feature.Value(3) 'MsgBox(feature.Value(0).ToString + " . " + feature.Value(1).ToString + " . " + feature.Value(2).ToString + " . " + feature.Value(3).ToString + " . " + feature.Value(4).ToString + " . ") feature = featureCursor1.NextFeature() i = i + 1 End While Marshal.FinalReleaseComObject(featureCursor1) MakeRoute.resultDistance = totlength Return routeNums End Function 'Create a new network anlaysis layer and set some solver settings Private Function CreateRouteAnalysisLayer(ByVal layerName As String, ByVal networkDataset As INetworkDataset) As INALayer Dim naRouteSolver As INARouteSolver = New NARouteSolverClass() Dim naSolverSettings As INASolverSettings = naRouteSolver Dim naSolver As INASolver = naRouteSolver 'Get the NetworkDataset's Data Element Dim datasetComponent As IDatasetComponent = networkDataset Dim deNetworkDataset As IDENetworkDataset = datasetComponent.DataElement 'Create the NAContext and bind to it Dim naContext As INAContext = naSolver.CreateContext(deNetworkDataset, layerName) Dim naContextEdit As INAContextEdit = naContext naContextEdit.Bind(networkDataset, New GPMessagesClass()) 'Create the NALayer Dim naLayer As INALayer = naSolver.CreateLayer(naContext) Dim layer As ILayer = naLayer layer.Name = layerName 'Set some properties on the the route solver interface naRouteSolver.FindBestSequence = True naRouteSolver.PreserveFirstStop = True naRouteSolver.PreserveLastStop = False naRouteSolver.UseTimeWindows = False naRouteSolver.OutputLines = esriNAOutputLineType.esriNAOutputLineTrueShapeWithMeasure 'Set some properties on the general INASolverSettings interface Dim restrictions As IStringArray = naSolverSettings.RestrictionAttributeNames 'Dim att As IStringArray = naSolverSettings.AccumulateAttributeNames 'att.Add("LEN_KM") 'naSolverSettings.AccumulateAttributeNames = att naSolverSettings.RestrictionAttributeNames = restrictions 'CODE TO USE HIERARCHY, or not naSolverSettings.UseHierarchy = MakeRoute.useHierarchy ' Update the context based on the changes made to the solver settings naSolver.UpdateContext(naContext, deNetworkDataset, New GPMessagesClass()) 'Return the layer Return naLayer End Function 'Write the NALayer out to disk as a layer file. Public Sub SaveLayerToDisk(ByVal layer As ILayer, ByVal path As String) Try Console.WriteLine("Writing layer file containing analysis to " + path) Dim layerfile As ILayerFile = New LayerFileClass() layerfile.New(path) layerfile.ReplaceContents(layer) layerfile.Save() Console.WriteLine("Writing layer file successfully saved") Catch err As Exception ' Write out errors Console.WriteLine(err.Message) TextFromFile1.errorwriter("save layer error: ", MakeRoute.person_no_global) End Try End Sub Again, this worked a month ago. I think the problem is with routeclass.init() . The rest of the code should be fine even if it doesn't follow conventional methods. I have initialised the licence for arcgis in another part of my code.
... View more
08-13-2013
08:29 AM
|
0
|
4
|
275
|
POST
|
Thank you Jeremiah. You explained it very well. I'm able to do everything quite easily using model builder. I've built a dataset and it works for the most part, there are some funny routes but I think this is down to the data quality. I also made a dataset without doing it in 3D, just offset the routes, and it seems to work the same. Can I ask you to put up screenshots of your Time evaluators? I followed the tutorial but I get errors when I try and route using my evaluator for time
... View more
08-09-2013
06:30 AM
|
0
|
0
|
5
|
POST
|
I didn't know about the variables. Thank you very much, that solved my problem.
... View more
08-09-2013
02:55 AM
|
0
|
0
|
2
|
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|