Imports ThinkGeo.UI.WinForms
Imports ThinkGeo.Core
Public Class Form1
  Public WithEvents MainMap As MapView
  Private WithEvents moExtentOverlay As ExtentInteractiveOverlay
  Private moShapefile As CustomShapefile
  Private moGeoTiffLayer As GeoTiffGdalRasterLayer
  Private moJpegLayer As Jpeg2000GdalRasterLayer
  Private moSelectionLayer As New InMemoryFeatureLayer
  Private moPageLayoutStuff As CreatePageLayout
  Private WithEvents moTrackOverlay As TrackInteractiveOverlay
  Private WithEvents moEditOverlay As CustomEditOverlay
  Private WithEvents moExistingControlPointsLayer As InMemoryFeatureLayer

  Public Sub New()
    InitializeComponent()
  End Sub
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    MainMap = New MapView With {
      .Dock = DockStyle.Fill
    }
    Controls.Add(MainMap)
    MainMap.BringToFront()

    Dim o As New CreatePageLayout(MainMap)

  End Sub

  Private Async Sub mAddMrSidLayer()
    Dim sLayerPath As String = "..\..\Data\DO_S13_7700_11.2021.sid"
    Dim oMrSidLayer As New MrSidGdalRasterLayer(sLayerPath)



    Dim oInt As New Projection(2925)
    Dim oExt As New Projection(3857)


    Dim PC As New GdalProjectionConverter(oInt.ProjString, oExt.ProjString)
    PC.Open()
    oMrSidLayer.ImageSource.ProjectionConverter = PC

    oMrSidLayer.Open()

    MainMap.MapUnit = GeographyUnit.Feet
    MainMap.CurrentExtent = oMrSidLayer.GetBoundingBox
    'MainMap.ExtentOverlay.PanMode = MapPanMode.Disabled
    Dim oOverlay As New LayerOverlay

    oOverlay.Layers.Add(oMrSidLayer)



    MainMap.Overlays.Add(oOverlay)

    Await MainMap.RefreshAsync()
  End Sub
  Private Async Sub mAddGeoTiffLayer()
    Dim sLayerPath As String = "..\..\Data\FloridaGeoTiff.tif"
    moGeoTiffLayer = New GeoTiffGdalRasterLayer(sLayerPath)
    'moGeoTiffLayer.KeyColors.Add(New GeoColor(0, 0, 0))

    Dim oInt As New Projection(4326)
    Dim oExt As New Projection(102003)


    Dim PC As New GdalProjectionConverter(oInt.ProjString, oExt.ProjString)
    PC.Open()
    moGeoTiffLayer.ImageSource.ProjectionConverter = PC

    moGeoTiffLayer.Open()

    MainMap.MapUnit = GeographyUnit.Meter
    MainMap.CurrentExtent = moGeoTiffLayer.GetBoundingBox
    'MainMap.ExtentOverlay.PanMode = MapPanMode.Disabled
    Dim oOverlay As New LayerOverlay
    oOverlay.TileType = TileType.SingleTile
    oOverlay.Layers.Add(moGeoTiffLayer)



    MainMap.Overlays.Add(oOverlay)

    Await MainMap.RefreshAsync()

  End Sub
  Private Async Sub mAddJpegLayer()
    Dim sLayerPath As String = "..\..\Data\FloridaJpeg.jpg"
    moJpegLayer = New Jpeg2000GdalRasterLayer(sLayerPath)
    'moJpegLayer.KeyColors.Add(New GeoColor(0, 0, 0)) ', New GeoColor(0, 0, 0))

    Dim oInt As New Projection(4326)
    Dim oExt As New Projection(102003)


    Dim PC As New GdalProjectionConverter(oInt.ProjString, oExt.ProjString)
    PC.Open()
    moJpegLayer.ImageSource.ProjectionConverter = PC

    moJpegLayer.Open()

    MainMap.MapUnit = GeographyUnit.Meter
    MainMap.CurrentExtent = moJpegLayer.GetBoundingBox
    'MainMap.ExtentOverlay.PanMode = MapPanMode.Disabled
    Dim oOverlay As LayerOverlay = CType(MainMap.Overlays("Layers"), LayerOverlay)
    oOverlay.TileType = TileType.SingleTile
    oOverlay.Layers.Add(moJpegLayer)



    'MainMap.Overlays.Add(oOverlay)

    Await MainMap.RefreshAsync()

  End Sub

  Private Sub mAddShapefile()
    moShapefile = New CustomShapefile("..\..\Data\States_Albers.shp")
    Dim oAreaStyle1 As AreaStyle = AreaStyle.CreateSimpleAreaStyle(GeoColors.Transparent, GeoColors.Blue, 1)
    Dim oAreaStyle2 As AreaStyle = AreaStyle.CreateSimpleAreaStyle(GeoColors.Transparent, GeoColors.Red, 1)
    ZoomLevelHelper.ApplyCustomZoomLevels(moShapefile.ZoomLevelSet, ZoomLevelHelper.EnumSymbolType.AreaSymbol)
    ZoomLevelHelper.ApplyCustomZoomLevels(moSelectionLayer.ZoomLevelSet, ZoomLevelHelper.EnumSymbolType.AreaSymbol)

    Dim iFirstHalf As Integer = 20
    Dim iSecondHalf = 38
    Dim iCounter As Integer = 0

    For Each oZoomLevel As ZoomLevel In moShapefile.ZoomLevelSet.CustomZoomLevels
      If iCounter < 20 Then
        oZoomLevel.DefaultAreaStyle = oAreaStyle1
      Else
        oZoomLevel.DefaultAreaStyle = oAreaStyle2
        'oZoomLevel.DefaultAreaStyle = Nothing
      End If
      iCounter = iCounter + 1
    Next oZoomLevel
    'moShapefile.DrawingQuality = DrawingQuality.HighSpeed
    'Dim oProj As New GdalProjectionConverter(102003, 3857)
    Dim sInternal As String = Projection.GetProjStringByEsriSrid(102003)
    Dim sExternal As String = Projection.GetProjStringByEpsgSrid(4326)
    Dim oProj As New GdalProjectionConverter(sInternal, sExternal)

    oProj.Open()
    'moShapefile.FeatureSource.ProjectionConverter = oProj

    Dim oSelectionStyle As AreaStyle = AreaStyle.CreateSimpleAreaStyle(GeoColors.LightGray, GeoColors.DimGray, 3)
    For Each oZoomLevel As ZoomLevel In moSelectionLayer.ZoomLevelSet.CustomZoomLevels
      oZoomLevel.DefaultAreaStyle = oSelectionStyle
    Next oZoomLevel

    moShapefile.SelectionLayer = moSelectionLayer


    Dim oOverlay As New LayerOverlay() With {.Name = "Layers", .TileType = TileType.SingleTile}
    MainMap.Overlays.Add("Layers", oOverlay)
    oOverlay.Layers.Add(moShapefile)

    'MainMap.CurrentExtent = oProj.ConvertToExternalProjection(New RectangleShape(-9762065.08512668, 3744836.02203581, -9681550.03146195, 3697034.6992771))
    MainMap.CurrentExtent = New RectangleShape(-9762065.08512668, 3744836.02203581, -9681550.03146195, 3697034.6992771)
    'Await MainMap.RefreshAsync
    btnSetMapPosition.Text = "Remove Projection"
  End Sub
  Private Async Sub mAddRemoveProjection(bRemove As Boolean)
    If bRemove Then
      btnSetMapPosition.Text = "Apply Projection"
      Dim oExtent As RectangleShape = MainMap.CurrentExtent
      If moGeoTiffLayer IsNot Nothing Then
        moGeoTiffLayer.ImageSource.ProjectionConverter = Nothing
      End If
      If moJpegLayer IsNot Nothing Then
        moJpegLayer.ImageSource.ProjectionConverter = Nothing
      End If
      If moShapefile IsNot Nothing Then
        Dim oProjConverter As New ProjectionConverter(102003, 4326)
        moShapefile.FeatureSource.ProjectionConverter = oProjConverter
        oProjConverter.Open()
        oExtent = oProjConverter.ConvertToExternalProjection(oExtent)
      End If
      MainMap.MapUnit = GeographyUnit.DecimalDegree
      MainMap.CurrentExtent = oExtent
      'MainMap.CurrentExtent = moGeoTiffLayer.GetBoundingBox
    Else
      Dim oExtent As RectangleShape = MainMap.CurrentExtent
      btnSetMapPosition.Text = "Remove Projection"
      If moShapefile IsNot Nothing Then
        moShapefile.FeatureSource.ProjectionConverter = Nothing
      End If
      Dim sExternalParams As String = Projection.GetProjStringByEsriSrid(102003)
      Dim sInternalParams As String = Projection.GetProjStringByEpsgSrid(4326)
      Dim oProjConverter As New GdalProjectionConverter(sInternalParams, sExternalParams)
      oProjConverter.Open()
      oExtent = oProjConverter.ConvertToExternalProjection(oExtent)
      If moGeoTiffLayer IsNot Nothing Then
        moGeoTiffLayer.ImageSource.ProjectionConverter = oProjConverter
      End If
      If moJpegLayer IsNot Nothing Then
        moJpegLayer.ImageSource.ProjectionConverter = oProjConverter
      End If
      MainMap.MapUnit = GeographyUnit.Meter
      MainMap.CurrentExtent = oExtent
    End If

    Await MainMap.RefreshAsync

  End Sub

  Private Async Sub btnSetMapPosition_Click(sender As Object, e As EventArgs) Handles btnSetMapPosition.Click
    Dim oPagePrinterLayer As PagePrinterLayer = Nothing
    Dim oMapPrinterLayer As MapPrinterLayer = Nothing

    Dim oPrinterOverlay As PrinterInteractiveOverlay = CType(MainMap.InteractiveOverlays("PrinterOverlay"), PrinterInteractiveOverlay)
    For Each oPrinterLayer As PrinterLayer In oPrinterOverlay.PrinterLayers
      If TypeOf oPrinterLayer Is PagePrinterLayer Then
        oPagePrinterLayer = CType(oPrinterLayer, PagePrinterLayer)
      End If
      If TypeOf oPrinterLayer Is MapPrinterLayer Then
        oMapPrinterLayer = CType(oPrinterLayer, MapPrinterLayer)
      End If
    Next oPrinterLayer


    Dim oLayerExtent As RectangleShape = oPagePrinterLayer.GetBoundingBox
    oLayerExtent.ScaleDown(10)
    oMapPrinterLayer.SetPosition(oLayerExtent)

    Await MainMap.RefreshAsync


  End Sub
End Class
