Skip to content

Commit

Permalink
Work on issue #21
Browse files Browse the repository at this point in the history
  • Loading branch information
lbross committed Nov 11, 2015
1 parent 9f9af86 commit c5bb097
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 30 deletions.
58 changes: 58 additions & 0 deletions src/BAGIS_ClassLibrary/PublicModule.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2232,4 +2232,62 @@ Optional ByVal hasPaddingBackSlach As Boolean = False) As String
End Try
End Function

' Verify that the vector dataset uses the same datum as the datum stored in the hruExtension
Public Function BA_VectorProjectionMatch(ByVal inputPath1 As String, ByVal inputPath2 As String) As Boolean
Dim pGeoDataset As IGeoDataset = Nothing
Dim pGeoDataset2 As IGeoDataset = Nothing
Dim spRef1 As ISpatialReference = Nothing
Dim spRef2 As ISpatialReference = Nothing
Try
Dim parentPath As String = "PleaseReturn"
Dim fileName As String = BA_GetBareName(inputPath1, parentPath)
Dim workspaceType As WorkspaceType = BA_GetWorkspaceTypeFromPath(inputPath1)
'Open the dataset to extract the spatial reference
If workspaceType = workspaceType.Raster Then
pGeoDataset = BA_OpenFeatureClassFromFile(parentPath, fileName)
ElseIf workspaceType = workspaceType.Geodatabase Then
pGeoDataset = BA_OpenFeatureClassFromGDB(parentPath, fileName)
End If
'Spatial reference for the dataset in question
spRef1 = pGeoDataset.SpatialReference

fileName = BA_GetBareName(inputPath2, parentPath)
workspaceType = BA_GetWorkspaceTypeFromPath(inputPath2)
'Open the dataset to extract the spatial reference
If workspaceType = workspaceType.Raster Then
pGeoDataset2 = BA_OpenFeatureClassFromFile(parentPath, fileName)
ElseIf workspaceType = workspaceType.Geodatabase Then
pGeoDataset2 = BA_OpenFeatureClassFromGDB(parentPath, fileName)
End If
If pGeoDataset2 IsNot Nothing Then
spRef2 = pGeoDataset2.SpatialReference
'Spatial reference for the dataset in question
If TypeOf spRef1 Is IProjectedCoordinateSystem Then
Dim proj1 As IProjectedCoordinateSystem = CType(spRef1, IProjectedCoordinateSystem)
Dim proj2 As IProjectedCoordinateSystem = TryCast(spRef2, IProjectedCoordinateSystem)
If proj2 IsNot Nothing Then
Return proj1.FactoryCode.Equals(proj2.FactoryCode)
End If
ElseIf TypeOf spRef1 Is IGeographicCoordinateSystem Then
Dim proj1 As IGeographicCoordinateSystem = CType(spRef1, IGeographicCoordinateSystem)
Dim proj2 As IProjectedCoordinateSystem = TryCast(spRef2, IGeographicCoordinateSystem)
If proj2 IsNot Nothing Then
Return proj1.FactoryCode.Equals(proj2.FactoryCode)
End If
End If
End If
Return False
Catch ex As Exception
Debug.Print("BA_VectorProjectionMatch Exception: " & ex.Message)
Return False
Finally
pGeoDataset = Nothing
pGeoDataset2 = Nothing
spRef1 = Nothing
spRef2 = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Try
End Function

End Module
2 changes: 2 additions & 0 deletions src/BAGIS_P/Forms/FrmPEandSRObs.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 29 additions & 9 deletions src/BAGIS_P/Forms/FrmPEandSRObs.vb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Public Class FrmPEandSRObs
Try
TxtAoiPath.Text = aoi.FilePath
BA_SetDefaultProjection(My.ArcMap.Application)
BA_SetDatumInExtension(TxtAoiPath.Text)
Me.Text = "Calculate PE and SR Obs parameters (AOI: " & aoi.Name & aoi.ApplicationVersion & " )"

Catch ex As Exception
Expand Down Expand Up @@ -61,6 +62,7 @@ Public Class FrmPEandSRObs
Dim aoiName As String = BA_GetBareName(DataPath)
Dim pAoi As Aoi = New Aoi(aoiName, DataPath, Nothing, bagisPExt.version)
TxtAoiPath.Text = pAoi.FilePath
BA_SetDatumInExtension(TxtAoiPath.Text)
'ResetForm()
Me.Text = "Calculate PE and SR Obs parameters (AOI: " & aoiName & pAoi.ApplicationVersion & " )"
bagisPExt.aoi = pAoi
Expand Down Expand Up @@ -96,7 +98,20 @@ Public Class FrmPEandSRObs
Dim pGxDataset As IGxDataset
pGxDataset = pGxObject.Next
Dim pDatasetName As IDatasetName = pGxDataset.DatasetName
TxtSrPath.Text = pDatasetName.WorkspaceName.PathName & "\" & pDatasetName.Name
Dim fullPath As String = pDatasetName.WorkspaceName.PathName & "\" & pDatasetName.Name

'Ensure selected layer uses the correct projection
Dim aoiGdb As String = BA_GeodatabasePath(TxtAoiPath.Text, GeodatabaseNames.Aoi, True)
Dim aoi_v As String = BA_StandardizeShapefileName(BA_EnumDescription(PublicPath.AoiVector), False)
Dim validSpatialReference As Boolean = BA_VectorProjectionMatch(fullPath, aoiGdb & aoi_v)

If validSpatialReference Then
TxtSrPath.Text = fullPath
Else
Dim bagisPExt As BagisPExtension = BagisPExtension.GetExtension
MessageBox.Show("The selected layer '" & pDatasetName.Name & "' cannot be used because the projection does not match the AOI projection. Please reproject or select another data layer and try again.", "Invalid projection", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

End Sub

Expand Down Expand Up @@ -135,8 +150,19 @@ Public Class FrmPEandSRObs
End If
End Sub

Private Sub ManageFileButtons()
If String.IsNullOrEmpty(TxtAoiPath.Text) Then
BtnSetSR.Enabled = False
BtnSetPE.Enabled = False
Else
BtnSetSR.Enabled = True
BtnSetPE.Enabled = True
End If
End Sub

Private Sub TxtAoiPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtAoiPath.TextChanged
ManageCalculateButton()
ManageFileButtons()
End Sub

Private Sub TxtSrPath_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtSrPath.TextChanged
Expand Down Expand Up @@ -167,14 +193,8 @@ Public Class FrmPEandSRObs
pArea = pFeature.Shape
pCentroid = pArea.Centroid
End If
Dim wType As WorkspaceType = BA_GetWorkspaceTypeFromPath(TxtSrPath.Text)
Dim folder As String = "PleaseReturn"
Dim fileName As String = BA_GetBareName(TxtSrPath.Text, folder)
If wType = WorkspaceType.Geodatabase Then

Else

End If
Dim oid As Integer = BA_FindClosestFeatureOID(pCentroid, TxtSrPath.Text)
MessageBox.Show(oid)
End If

End If
Expand Down
23 changes: 2 additions & 21 deletions src/BAGIS_P/Forms/FrmProfileBuilder.vb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Public Class FrmProfileBuilder
Me.Text = "Profile Builder (AOI: " & aoi.Name & m_aoi.ApplicationVersion & " )"

BA_SetDefaultProjection(My.ArcMap.Application)
SetDatumInExtension()
BA_SetDatumInExtension(m_aoi.FilePath)

Try
' Get the count of zone directories so we know how many steps to put into the StepProgressor
Expand Down Expand Up @@ -231,7 +231,7 @@ Public Class FrmProfileBuilder
'ResetForm()
Me.Text = "Profile Builder (AOI: " & aoiName & m_aoi.ApplicationVersion & " )"
bagisPExt.aoi = m_aoi
SetDatumInExtension()
BA_SetDatumInExtension(m_aoi.FilePath)

' Get the count of zone directories so we know how many steps to put into the StepProgressor
' Create a DirectoryInfo of the HRU directory.
Expand Down Expand Up @@ -280,25 +280,6 @@ Public Class FrmProfileBuilder
End Try
End Sub

' Sets the datum string from the source DEM in the hru extension
Private Sub SetDatumInExtension()
Dim bagisPExt As BagisPExtension = BagisPExtension.GetExtension
Dim parentPath As String = m_aoi.FilePath & "\" & BA_EnumDescription(GeodatabaseNames.Surfaces)
Dim pGeoDataSet As IGeoDataset = BA_OpenRasterFromGDB(parentPath, BA_EnumDescription(MapsFileName.filled_dem_gdb))
If pGeoDataSet IsNot Nothing Then
'Spatial reference for the dataset in question
Dim pSpRef As ESRI.ArcGIS.Geometry.ISpatialReference = pGeoDataSet.SpatialReference
If pSpRef IsNot Nothing Then
bagisPExt.Datum = BA_DatumString(pSpRef)
bagisPExt.SpatialReference = pSpRef.Name
End If
pSpRef = Nothing
End If
pGeoDataSet = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub

Private Sub LoadHruGrid(ByVal dirZonesArr As DirectoryInfo())
GrdHruLayers.Rows.Clear()
If dirZonesArr IsNot Nothing Then
Expand Down
46 changes: 46 additions & 0 deletions src/BAGIS_P/ParameterModule.vb
Original file line number Diff line number Diff line change
Expand Up @@ -1603,4 +1603,50 @@ Module ParameterModule
Return keysA
End Function

Public Function BA_FindClosestFeatureOID(ByVal searchGeo As IGeometry, ByVal targetFeatureClassPath As String) As Integer
Dim targetFolder As String = "PleaseReturn"
Dim targetFile As String = BA_GetBareName(targetFeatureClassPath, targetFolder)
Dim featureClass As IFeatureClass = Nothing
Dim fCursor As IFeatureCursor = Nothing
Dim queryFeature As IFeature = Nothing
Dim queryGeometry As IGeometry = Nothing
Dim proxOperator As IProximityOperator = Nothing
Dim closestOID As Integer = -1
Dim closestDistance As Double = -1
Try
Dim wType As WorkspaceType = BA_GetWorkspaceTypeFromPath(targetFeatureClassPath)
If wType = WorkspaceType.Geodatabase Then
featureClass = BA_OpenFeatureClassFromGDB(targetFolder, targetFile)
Else
featureClass = BA_OpenFeatureClassFromFile(targetFolder, targetFile)
End If
If featureClass IsNot Nothing Then
fCursor = featureClass.Search(Nothing, False)
If fCursor IsNot Nothing Then
queryFeature = fCursor.NextFeature
Do While queryFeature IsNot Nothing
queryGeometry = queryFeature.Shape
proxOperator = CType(queryGeometry, IProximityOperator)
Dim distance As Double = proxOperator.ReturnDistance(searchGeo)
If closestDistance < 0 Then
closestDistance = distance
closestOID = queryFeature.OID
ElseIf distance < closestDistance Then
closestDistance = distance
closestOID = queryFeature.OID
End If
queryFeature = fCursor.NextFeature
Loop
Return closestOID
End If
End If
Return closestOID
Catch ex As Exception
Debug.Print("BA_FindClosestFeatureOID: " & ex.Message)
Return -1
Finally
'@ToDo: Release object references
End Try
End Function

End Module
19 changes: 19 additions & 0 deletions src/BAGIS_P/ProfileModule.vb
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,23 @@ Module ProfileModule
Return BA_ReturnCode.Success
End Function

' Sets the datum string from the source DEM in the BAGIS-P extension
Public Sub BA_SetDatumInExtension(ByVal aoiFilePath As String)
Dim bagisPExt As BagisPExtension = BagisPExtension.GetExtension
Dim parentPath As String = aoiFilePath & "\" & BA_EnumDescription(GeodatabaseNames.Surfaces)
Dim pGeoDataSet As IGeoDataset = BA_OpenRasterFromGDB(parentPath, BA_EnumDescription(MapsFileName.filled_dem_gdb))
If pGeoDataSet IsNot Nothing Then
'Spatial reference for the dataset in question
Dim pSpRef As ESRI.ArcGIS.Geometry.ISpatialReference = pGeoDataSet.SpatialReference
If pSpRef IsNot Nothing Then
bagisPExt.Datum = BA_DatumString(pSpRef)
bagisPExt.SpatialReference = pSpRef.Name
End If
pSpRef = Nothing
End If
pGeoDataSet = Nothing
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub

End Module

0 comments on commit c5bb097

Please sign in to comment.