[Gdal-dev] OGR_csharp sample code
Mateusz Loskot
mateusz at loskot.net
Fri Jun 1 10:00:22 EDT 2007
Folks,
FYI, if you like, you can add interesting snippets to the GDAL Wiki:
http://trac.osgeo.org/gdal/wiki/CodeSnippets
Mateusz
Milo van der Linden wrote:
> Hello,
>
> I have added some of the source I am using in VB.net to do exactly as
> what you are requesting. There are a couple of things you have to keep
> in mind:
> - Build enumerators that mimic the enumerators that are in the original
> OGR dll's
> - Be careful, base for featurecount are different for shape files or
> mapinfo tab (the first starts at 0, the other at 1)
>
> Let me be honest, I am not giving you the code in detail. Google Earth
> content is core business for my own little company (3DSite,
> http://www.3dsite.nl)
>
> I have spent a lot of time getting it to what it is now, I am not
> working for a boss beside this and nobody is funding my effort. Maybe
> you can give me some insight on what you are going to use it for?
> Perhaps we can partner in the business, Greece is not to far away from
> the Netherlands and I am a great fan of Greece!
>
>
> First, at the loading of my vb.net application I register all OGR file
> types:
>
> 'Register all filetypes for use with OGR
> OGR.ogr.RegisterAll()
>
> I have a Class that holds the following objects:
>
> Datasets Collection of type Dataset (selfmade class)
>
> Dataset looks something like this:
>
> Public Class Dataset
> Private _DataSource As OGR.DataSource
> Private _Driver As OGR.Driver
> Private _Name As String
> Private _Path As String
> Private _Extension As String
> Private _FileName As String
> Private _Layers As Layers
> Private _NameField As String
> Private _GroupField As String
> Private _SortField As String
>
> where I turn all these private variables into public properties
>
> Then I have 2 constructors in the dataset object:
>
> Public Sub New(ByVal name As String, ByVal path As String, ByVal
> extension As String)
> _Name = name
> _Path = path
> _Extension = extension
> _FileName = _Path & "\" & _Name & _Extension
> _DataSource = OGR.ogr.Open(_FileName, 0)
> _Driver = _DataSource.GetDriver()
>
> End Sub
> Public Sub New(ByVal FileName As String)
> Dim j As Integer
> _Layers = New Layers
> _FileName = FileName
> _Path = System.IO.Path.GetDirectoryName(FileName)
> _Extension = System.IO.Path.GetExtension(FileName)
> _Name = System.IO.Path.GetFileNameWithoutExtension(FileName)
> _DataSource = OGR.ogr.Open(_FileName, 0)
> _Driver = _DataSource.GetDriver()
> For j = 0 To _DataSource.GetLayerCount - 1
> Dim lyr As New Layer(_DataSource.GetLayerByIndex(j))
> _Layers.Add(lyr)
> Next
> End Sub
>
> The Layer object is an enhanced version of the OGR.Layer object:
>
> Public Class Layer
> Private _fieldtable As DataTable = New DataTable("Fields")
> Private _sr As New OGR.SpatialReference("")
> Private _ogrLayer As OGR.Layer
> Private _name As String
> Private _def As OGR.FeatureDefn
> Private _FeatureCount As Integer
> Private _Type As Integer
>
> The Layer Object also has an advanced constructor:
>
> Public Sub New(ByVal lyr As OGR.Layer)
>
> ' Create DataColumns and set various properties.
> Dim fName As DataColumn = New DataColumn
> fName.DataType = System.Type.GetType("System.String")
> fName.AllowDBNull = True
> fName.Caption = "Name"
> fName.ColumnName = "name"
> fName.DefaultValue = ""
> ' Add the column to the table.
> _fieldtable.Columns.Add(fName)
>
> Dim fNum As DataColumn = New DataColumn
> fNum.DataType = System.Type.GetType("System.String")
> fNum.AllowDBNull = True
> fNum.Caption = "Type"
> fNum.ColumnName = "type"
> fNum.DefaultValue = 0
> ' Add the column to the table.
> _fieldtable.Columns.Add(fNum)
>
> Dim tmp As String = ""
> _ogrLayer = lyr
>
> _def = _ogrLayer.GetLayerDefn()
> _name = _def.GetName
> _FeatureCount = _ogrLayer.GetFeatureCount(1)
> _Type = _def.GetGeomType
> Dim i As Integer
> For i = 0 To _def.GetFieldCount() - 1
> Dim row As DataRow
> row = _fieldtable.NewRow()
> Dim FieldDef As OGR.FieldDefn = _def.GetFieldDefn(i)
> row("name") = FieldDef.GetName
> row("type") = FieldDef.GetFieldType
> _fieldtable.Rows.Add(row)
> Next
> _sr = _ogrLayer.GetSpatialRef
> _sr.ExportToProj4(tmp)
> If tmp = "" Then
> Dim ATproj4 As String = "+proj=tmerc +lat_0=12.51697
> +lon_0=-69.994775 +k=0.9996 +x_0=10000 +y_0=15000 +towgs84=0,0,0,0,0,0,0
> +units=m +no_defs"
> ' 'Pop up a dialog stating that the source projection
> is invalid!
> ' '"No Source coordinate system defined, proceed?"
> _sr.ImportFromProj4(ATproj4)
> Else
> Debug.Print("Source coordinate system: " & tmp)
> End If
> End Sub
>
> Then, for the conversion to KML, I loop through this Dataset-Layer object
> Dim lyr As ClassData.Layer
> For Each lyr In _ds.Layers
> count = lyr.Count
> For j = i To count
> Dim feat As OGR.Feature = lyr.Feature(j)
> If Not feat Is Nothing Then
> Dim geom As OGR.Geometry = feat.GetGeometryRef
> If Not geom Is Nothing Then
> Dim geomType As wkbGeometryType
> geomType = geom.GetGeometryType
> If geomType = wkbGeometryType.wkbLineString
> Or geomType = wkbGeometryType.wkbPolygon Or geomType =
> wkbGeometryType.wkbPoint Or geomType = wkbGeometryType.wkbMultiPolygon Then
> ..... Now you have reached the point
> where you have a valid object!, use it to convert to a kml string
>
>
> Next
>
> IMPORTANT:
>
> If you are using non US system settings: You can use this code to make
> sure comma's are interpreted correctly:
>
> Sub DoTheThing
> 'First, store the CurrentCulture so it can be reset after the
> routine is done
> Dim originalCulture As System.Globalization.CultureInfo
> originalCulture = CultureInfo.CurrentCulture
>
> 'Google Earth needs the locale to be set to US so the . and
> , are interpreted correctly
> Thread.CurrentThread.CurrentCulture = New
> CultureInfo("en-US", True)
>
> DoTheConversionHere
>
> 'Reset the CurrentCulture
> Thread.CurrentThread.CurrentCulture = originalCulture
> end sub
>
>
> Kind regards,
>
> Milo van der Linden
>
> 3DSite - Milo van der Linden - CTO
> Groenedijk 12, 4441 TJ Ovezande (Zeeland, Nederland)
> milo at 3dsite.nl <mailto:milo at 3dsite.nl>
> http://www.3dsite.nl
>
>
> <skype:milovanderlinden?chat>
>
>
>
> De informatie verzonden met dit e-mail bericht is uitsluitend bestemd
> voor de geadresseerde(n). Gebruik van deze informatie door andere dan de
> geadresseerde(n) is verboden. Openbaarmaking, vermenigvuldiging,
> verspreiding en/of verstrekking van deze informatie aan derde is niet
> toegestaan. Kopiëren en/of misbruiken van ingesloten afbeeldingen en/of
> bestanden is wettelijke verboden en strafbaar. 3DSite staat niet in voor
> de juiste en volledige overbrenging van de inhoud van een verzonden
> e-mail, noch voor tijdig ontvangst daarvan. Op al onze leveringen,
> aanbiedingen en verkopen zijn van toepassing onze Algemene Voorwaarden,
> zoals gedeponeerd bij de K.v.K. te Middelburg. 3DSite
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Gdal-dev mailing list
> Gdal-dev at lists.maptools.org
> http://lists.maptools.org/mailman/listinfo/gdal-dev
--
Mateusz Loskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list