Mini Kabibi Habibi
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Resources
Imports System.Xml.Linq
Imports DevExpress.Xpf.Map
Imports System.Globalization
Imports System.Runtime.Serialization
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Net
Imports System.IO
Imports System.Threading.Tasks
Imports System.Runtime.Serialization.Json
Imports System.Windows.Threading
Namespace MapDemo
Public Enum TemperatureScale
Fahrenheit
Celsius
End Enum
Public Class DemoValuesProvider
Private Const key As String = "AmSNFwVzMvaqFlCYQx9RRUfcAwSQCzi_Vcesric6JFQuBO9wZFXEsqzili-INaUA"
Public ReadOnly Property DevexpressBingKey() As String
Get
Return key
End Get
End Property
Public ReadOnly Property BingMapKinds() As IEnumerable(Of BingMapKind)
Get
Return New BingMapKind() { BingMapKind.Area, BingMapKind.Road, BingMapKind.Hybrid }
End Get
End Property
Public ReadOnly Property ShapeMapTypes() As IEnumerable(Of String)
Get
Return New String() { "GDP", "Population", "Political" }
End Get
End Property
Public ReadOnly Property ShapefileMapTypes() As IEnumerable(Of String)
Get
Return New String() { "World", "Africa", "South America", "North America", "Australia", "Eurasia" }
End Get
End Property
Public ReadOnly Property TemperatureUnit() As IEnumerable(Of TemperatureScale)
Get
Return New TemperatureScale() { TemperatureScale.Celsius, TemperatureScale.Fahrenheit }
End Get
End Property
End Class
Public NotInheritable Class DataLoader
Private Sub New()
End Sub
Public Shared Function LoadXmlFromResources(ByVal fileName As String) As XDocument
Try
Return XDocument.Load("/MapDemo;component" & fileName)
Catch
Return Nothing
End Try
End Function
End Class
Public Class DoubleToTimeSpanConvert
Implements IValueConverter
#Region "IValueConvector implementation"
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim doubleValue As Double = 3600 * CDbl(value)
Return New TimeSpan(0, 0, CInt(Fix(Math.Ceiling(doubleValue))))
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Return Nothing
End Function
#End Region
End Class
Public Class RangeColor
Private ReadOnly rangeMin_Renamed As Integer
Private ReadOnly rangeMax_Renamed As Integer
Private ReadOnly fill_Renamed As Color
Public ReadOnly Property RangeMin() As Integer
Get
Return rangeMin_Renamed
End Get
End Property
Public ReadOnly Property RangeMax() As Integer
Get
Return rangeMax_Renamed
End Get
End Property
Public ReadOnly Property Fill() As Color
Get
Return fill_Renamed
End Get
End Property
Public Sub New(ByVal rangeMin As Integer, ByVal rangeMax As Integer, ByVal fill As Color)
Me.rangeMin_Renamed = rangeMin
Me.rangeMax_Renamed = rangeMax
Me.fill_Renamed = fill
End Sub
End Class
Public Class ViewTypeToBoolConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.Convert
If targetType Is GetType(Boolean) AndAlso TypeOf value Is ViewType AndAlso TypeOf parameter Is ViewType Then
Return CType(value, ViewType) = CType(parameter, ViewType)
End If
Return False
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
If TypeOf value Is Boolean Then
If targetType Is GetType(ViewType) Then
Return If(CBool(value), ViewType.Gallery, ViewType.Map)
End If
End If
Return Nothing
End Function
End Class
Public Class ShapefileWorldResources
Public ReadOnly Property CountriesFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/Countries.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public ReadOnly Property AfricaFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/Africa.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public ReadOnly Property SouthAmericaFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/SouthAmerica.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public ReadOnly Property NorthAmericaFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/NorthAmerica.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public ReadOnly Property AustraliaFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/Australia.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public ReadOnly Property EurasiaFileUri() As Uri
Get
Return New Uri("/MapDemo;component/Data/Shapefiles/Maps/Eurasia.shp", UriKind.RelativeOrAbsolute)
End Get
End Property
Public Sub New()
End Sub
End Class
Public Class PhotoGalleryResources
Public ReadOnly Property CityInformationControlSource() As BitmapImage
Get
Return New BitmapImage(New Uri("/MapDemo;component/Images/PhotoGallery/CityInformationControl.png", UriKind.RelativeOrAbsolute))
End Get
End Property
Public ReadOnly Property LabelControlImageSource() As BitmapImage
Get
Return New BitmapImage(New Uri("/MapDemo;component/Images/PhotoGallery/Label.png", UriKind.RelativeOrAbsolute))
End Get
End Property
Public ReadOnly Property PlaceInfoControlPrevImageSource() As BitmapImage
Get
Return New BitmapImage(New Uri("/MapDemo;component/Images/PhotoGallery/PrevPlace.png", UriKind.RelativeOrAbsolute))
End Get
End Property
Public ReadOnly Property PlaceInfoControlNextImageSource() As BitmapImage
Get
Return New BitmapImage(New Uri("/MapDemo;component/Images/PhotoGallery/NextPlace.png", UriKind.RelativeOrAbsolute))
End Get
End Property
Public Sub New()
End Sub
End Class
Public Class CityWeather
Implements INotifyPropertyChanged
Private cityWeatherInfo As OpenWeatherMapService.CityWeatherInfo
Private weatherIconPath_Renamed As String
Private weather_Renamed As Weather
Private forecast_Renamed As ObservableCollection(Of CityWeather)
Private weatherDescriptions_Renamed As List(Of WeatherDescription)
Private temperatureValueDataMember_Renamed As String
Private temperatureString_Renamed As String
Private crosshairLabelPattern_Renamed As String
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public ReadOnly Property DateTime() As DateTime
Get
Return GetTime(cityWeatherInfo.DateTime)
End Get
End Property
Public ReadOnly Property CityID() As Integer
Get
Return cityWeatherInfo.Id
End Get
End Property
Public ReadOnly Property City() As String
Get
Return cityWeatherInfo.Name
End Get
End Property
Public ReadOnly Property Longitude() As Double
Get
Return cityWeatherInfo.Coord.Longitude
End Get
End Property
Public ReadOnly Property Latitude() As Double
Get
Return cityWeatherInfo.Coord.Latitude
End Get
End Property
Public ReadOnly Property Weather() As Weather
Get
Return weather_Renamed
End Get
End Property
Public ReadOnly Property WeatherDescriptions() As List(Of WeatherDescription)
Get
Return weatherDescriptions_Renamed
End Get
End Property
Private privateForecastTime As DateTime
Public Property ForecastTime() As DateTime
Get
Return privateForecastTime
End Get
Set(ByVal value As DateTime)
privateForecastTime = value
End Set
End Property
Public Property WeatherIconPath() As String
Get
Return weatherIconPath_Renamed
End Get
Set(ByVal value As String)
If weatherIconPath_Renamed <> value Then
weatherIconPath_Renamed = value
OnPropertyChanged("WeatherIconPath")
End If
End Set
End Property
Public Property Forecast() As ObservableCollection(Of CityWeather)
Get
Return forecast_Renamed
End Get
Set(ByVal value As ObservableCollection(Of CityWeather))
If forecast_Renamed IsNot value Then
forecast_Renamed = value
OnPropertyChanged("Forecast")
End If
End Set
End Property
Public Property TemperatureValueDataMember() As String
Get
Return temperatureValueDataMember_Renamed
End Get
Set(ByVal value As String)
If temperatureValueDataMember_Renamed <> value Then
temperatureValueDataMember_Renamed = value
OnPropertyChanged("TemperatureValueDataMember")
End If
End Set
End Property
Public Property TemperatureString() As String
Get
Return temperatureString_Renamed
End Get
Set(ByVal value As String)
If temperatureString_Renamed <> value Then
temperatureString_Renamed = value
OnPropertyChanged("TemperatureString")
End If
End Set
End Property
Public Property CrosshairLabelPattern() As String
Get
Return crosshairLabelPattern_Renamed
End Get
Set(ByVal value As String)
If crosshairLabelPattern_Renamed <> value Then
crosshairLabelPattern_Renamed = value
OnPropertyChanged("CrosshairLabelPattern")
End If
End Set
End Property
Public Sub New(ByVal cityWeatherInfo As OpenWeatherMapService.CityWeatherInfo)
Me.cityWeatherInfo = cityWeatherInfo
Me.weather_Renamed = New Weather(cityWeatherInfo.Main)
Me.weatherDescriptions_Renamed = New List(Of WeatherDescription)()
For Each weatherDescription As OpenWeatherMapService.WeatherDescriptionInfo In cityWeatherInfo.Weather
weatherDescriptions_Renamed.Add(New WeatherDescription(weatherDescription))
Next weatherDescription
End Sub
Private Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
Private Function GetTime(ByVal seconds As Long) As DateTime
Dim dtDateTime As New DateTime(1970, 1, 1, 0, 0, 0, 0)
Return dtDateTime.AddSeconds(seconds).ToLocalTime()
End Function
Public Sub SetForecast(ByVal forecast As ObservableCollection(Of OpenWeatherMapService.CityWeatherInfo))
Dim cityWeatherList As New ObservableCollection(Of CityWeather)()
For Each cityWeatherInfo As OpenWeatherMapService.CityWeatherInfo In forecast
cityWeatherList.Add(New CityWeather(cityWeatherInfo))
Next cityWeatherInfo
Me.Forecast = cityWeatherList
End Sub
Public Sub SetCurrentTemperatureType(ByVal temperatureScale As TemperatureScale)
Select Case temperatureScale
Case TemperatureScale.Fahrenheit
TemperatureValueDataMember = "Weather.FahrenheitTemperature"
TemperatureString = Weather.FahrenheitTemperatureString
CrosshairLabelPattern = "{A:g} : {V} °F"
Case TemperatureScale.Celsius
TemperatureValueDataMember = "Weather.CelsiusTemperature"
TemperatureString = Weather.CelsiusTemperatureString
CrosshairLabelPattern = "{A:g} : {V} °C"
End Select
End Sub
End Class
Public Class Weather
Private weatherInfo As OpenWeatherMapService.WeatherInfo
Public ReadOnly Property CelsiusTemperature() As Integer
Get
Return CInt(Fix(weatherInfo.Temp - 273.15))
End Get
End Property
Public ReadOnly Property FahrenheitTemperature() As Integer
Get
Return CInt(Fix(weatherInfo.Temp - 273.15)) * 9 \ 5 + 32
End Get
End Property
Public ReadOnly Property KelvinTemperature() As Integer
Get
Return CInt(Fix(weatherInfo.Temp))
End Get
End Property
Public ReadOnly Property CelsiusTemperatureString() As String
Get
Return CelsiusTemperature.ToString("+#;-#;0") & " °C"
End Get
End Property
Public ReadOnly Property FahrenheitTemperatureString() As String
Get
Return FahrenheitTemperature.ToString("+#;-#;0") & " °F"
End Get
End Property
Public ReadOnly Property KelvinTemperatureString() As String
Get
Return weatherInfo.Temp.ToString("+#;-#;0") & " °K"
End Get
End Property
Public Sub New(ByVal weatherInfo As OpenWeatherMapService.WeatherInfo)
Me.weatherInfo = weatherInfo
End Sub
End Class
Public Class WeatherDescription
Private weatherDescriptionInfo As OpenWeatherMapService.WeatherDescriptionInfo
Public ReadOnly Property IconName() As String
Get
Return weatherDescriptionInfo.Icon
End Get
End Property
Public Sub New(ByVal weatherDescriptionInfo As OpenWeatherMapService.WeatherDescriptionInfo)
Me.weatherDescriptionInfo = weatherDescriptionInfo
End Sub
End Class
Public Class OpenWeatherMapService
Implements INotifyPropertyChanged
#Region "classes for JSON parsing"
<DataContract> _
Public Class ForecastInfo
<DataMember> _
Public list As ObservableCollection(Of CityWeatherInfo)
End Class
<DataContract> _
Public Class WorldWeatherInfo
<DataMember> _
Public list As ObservableCollection(Of CityWeatherInfo)
End Class
<DataContract> _
Public Class CityWeatherInfo
Private privateId As Integer
<DataMember(Name := "id")> _
Public Property Id() As Integer
Get
Return privateId
End Get
Set(ByVal value As Integer)
privateId = value
End Set
End Property
Private privateName As String
<DataMember(Name := "name")> _
Public Property Name() As String
Get
Return privateName
End Get
Set(ByVal value As String)
privateName = value
End Set
End Property
Private privateCoord As Coordinates
<DataMember(Name := "coord")> _
Public Property Coord() As Coordinates
Get
Return privateCoord
End Get
Set(ByVal value As Coordinates)
privateCoord = value
End Set
End Property
Private privateMain As WeatherInfo
<DataMember(Name := "main")> _
Public Property Main() As WeatherInfo
Get
Return privateMain
End Get
Set(ByVal value As WeatherInfo)
privateMain = value
End Set
End Property
Private privateWeather As List(Of WeatherDescriptionInfo)
<DataMember(Name := "weather")> _
Public Property Weather() As List(Of WeatherDescriptionInfo)
Get
Return privateWeather
End Get
Set(ByVal value As List(Of WeatherDescriptionInfo))
privateWeather = value
End Set
End Property
Private privateWind As WindInfo
<DataMember(Name := "wind")> _
Public Property Wind() As WindInfo
Get
Return privateWind
End Get
Set(ByVal value As WindInfo)
privateWind = value
End Set
End Property
Private privateDateTime As Long
<DataMember(Name := "dt")> _
Public Property DateTime() As Long
Get
Return privateDateTime
End Get
Set(ByVal value As Long)
privateDateTime = value
End Set
End Property
End Class
<DataContract> _
Public Class WeatherDescriptionInfo
Private privateMain As String
<DataMember(Name := "main")> _
Public Property Main() As String
Get
Return privateMain
End Get
Set(ByVal value As String)
privateMain = value
End Set
End Property
Private privateDescription As String
<DataMember(Name := "description")> _
Public Property Description() As String
Get
Return privateDescription
End Get
Set(ByVal value As String)
privateDescription = value
End Set
End Property
Private privateIcon As String
<DataMember(Name := "icon")> _
Public Property Icon() As String
Get
Return privateIcon
End Get
Set(ByVal value As String)
privateIcon = value
End Set
End Property
End Class
<DataContract> _
Public Class WindInfo
Private privateSpeed As Double
<DataMember(Name := "speed")> _
Public Property Speed() As Double
Get
Return privateSpeed
End Get
Set(ByVal value As Double)
privateSpeed = value
End Set
End Property
Private privateDeg As Double
<DataMember(Name := "deg")> _
Public Property Deg() As Double
Get
Return privateDeg
End Get
Set(ByVal value As Double)
privateDeg = value
End Set
End Property
End Class
<DataContract> _
Public Class WeatherInfo
Private privateTemp As Double
<DataMember(Name := "temp")> _
Public Property Temp() As Double
Get
Return privateTemp
End Get
Set(ByVal value As Double)
privateTemp = value
End Set
End Property
Private privatePressure As Double
<DataMember(Name := "pressure")> _
Public Property Pressure() As Double
Get
Return privatePressure
End Get
Set(ByVal value As Double)
privatePressure = value
End Set
End Property
Private privateHumidity As Double
<DataMember(Name := "humidity")> _
Public Property Humidity() As Double
Get
Return privateHumidity
End Get
Set(ByVal value As Double)
privateHumidity = value
End Set
End Property
End Class
<DataContract> _
Public Class Coordinates
Private privateLongitude As Double
<DataMember(Name := "lon")> _
Public Property Longitude() As Double
Get
Return privateLongitude
End Get
Set(ByVal value As Double)
privateLongitude = value
End Set
End Property
Private privateLatitude As Double
<DataMember(Name := "lat")> _
Public Property Latitude() As Double
Get
Return privateLatitude
End Get
Set(ByVal value As Double)
privateLatitude = value
End Set
End Property
End Class
#End Region
Private temperatureScale As TemperatureScale = TemperatureScale.Celsius
Private weatherLocker As Object = New Object()
Private weatherInCities_Renamed As ObservableCollection(Of CityWeather)
Private capitals As New List(Of String)()
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property WeatherInCities() As ObservableCollection(Of CityWeather)
Get
Return weatherInCities_Renamed
End Get
Set(ByVal value As ObservableCollection(Of CityWeather))
If weatherInCities_Renamed IsNot value Then
weatherInCities_Renamed = value
OnPropertyChanged("WeatherInCities")
End If
End Set
End Property
Private privateForecast As ObservableCollection(Of CityWeather)
Public Property Forecast() As ObservableCollection(Of CityWeather)
Get
Return privateForecast
End Get
Set(ByVal value As ObservableCollection(Of CityWeather))
privateForecast = value
End Set
End Property
Public Sub New()
LoadCapitalsFromXML()
End Sub
Private Sub weatherClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
If e.Error Is Nothing Then
Task.Factory.StartNew(Function() AnonymousMethod1(e))
End If
End Sub
Private Function AnonymousMethod1(ByVal e As OpenReadCompletedEventArgs) As Boolean
Dim dc As New DataContractJsonSerializer(GetType(WorldWeatherInfo))
Dim worldWeatherInfo As WorldWeatherInfo = CType(dc.ReadObject(e.Result), WorldWeatherInfo)
Dim tempWeatherInCities As New ObservableCollection(Of CityWeather)()
For Each weatherInfo As CityWeatherInfo In worldWeatherInfo.list
Dim cityWeather As New CityWeather(weatherInfo)
If capitals.Contains(cityWeather.City) Then
If cityWeather.WeatherDescriptions IsNot Nothing AndAlso cityWeather.WeatherDescriptions.Count > 0 Then
cityWeather.WeatherIconPath = "http://openweathermap.org/img/w/" & cityWeather.WeatherDescriptions(0).IconName & ".png"
End If
tempWeatherInCities.Add(cityWeather)
End If
Next weatherInfo
SyncLock weatherLocker
WeatherInCities = tempWeatherInCities
End SyncLock
UpdateCurrentTemperatureType()
Return True
End Function
Private Sub forecastClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs)
If e.Error Is Nothing Then
RemoveHandler (CType(sender, WebClient)).OpenReadCompleted, AddressOf forecastClient_OpenReadCompleted
Dim stream As Stream = e.Result
Dim cityWeatherInfo As CityWeather = CType(e.UserState, CityWeather)
Task.Factory.StartNew(Function() AnonymousMethod2(stream, cityWeatherInfo))
End If
End Sub
Private Function AnonymousMethod2(ByVal stream As Stream, ByVal cityWeatherInfo As CityWeather) As Boolean
Dim dc As New DataContractJsonSerializer(GetType(ForecastInfo))
Dim forecast As ForecastInfo = CType(dc.ReadObject(stream), ForecastInfo)
cityWeatherInfo.SetForecast(forecast.list)
Return True
End Function
Private Sub LoadCapitalsFromXML()
Dim document As XDocument = DataLoader.LoadXmlFromResources("/Data/Capitals.xml")
If document IsNot Nothing Then
For Each element As XElement In document.Element("Capitals").Elements()
capitals.Add(element.Value)
Next element
End If
End Sub
Private Sub OnPropertyChanged(ByVal propertyName As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Private Sub UpdateCurrentTemperatureType()
SyncLock weatherLocker
If WeatherInCities IsNot Nothing Then
For Each weather As CityWeather In WeatherInCities
weather.SetCurrentTemperatureType(temperatureScale)
Next weather
End If
End SyncLock
End Sub
Public Sub GetWeatherAsync()
Dim link As String = "http://api.openweathermap.org/data/2.1/find/city?bbox=-180,-90,180,90"
Dim weatherClient As New WebClient()
AddHandler weatherClient.OpenReadCompleted, AddressOf weatherClient_OpenReadCompleted
weatherClient.OpenReadAsync(New Uri(link))
End Sub
Public Sub GetForecastForCityAsync(ByVal cityWeather As CityWeather)
Dim link As String = "http://api.openweathermap.org/data/2.1/forecast/city/" & cityWeather.CityID.ToString()
Dim forecastClient As New WebClient()
AddHandler forecastClient.OpenReadCompleted, AddressOf forecastClient_OpenReadCompleted
forecastClient.OpenReadAsync(New Uri(link), cityWeather)
End Sub
Public Sub SetCurrentTemperatureType(ByVal temperatureScale As TemperatureScale)
Me.temperatureScale = temperatureScale
UpdateCurrentTemperatureType()
End Sub
End Class
End Namespace