Archive

Archive for November, 2010

New Spatial Features in SQL Server “Denali”

November 22, 2010 Leave a comment

A community preview of sql server adds support for curved objects and objects larger than a hemisphere.

Advertisements
Categories: Uncategorized

Linq – Multiple sources

November 15, 2010 Leave a comment

MSDN has a linq sample that shows how to work with multiple sources. This also works with nested collections. Assuming class A is defined as:

class A

{

    public A() { Collection = new List<string>(); }

    public int Id { get; set; }

    public List<string> Collection { get; set; }

}

 

and a collection of A:s,

var a1 = new A { Id = 1, Collection = { “B11”, “B12”, “B13” } };

var a2 = new A { Id = 2, Collection = { “B21”, “B22”, “B23” } };

var a3 = new A { Id = 3, Collection = { “B31”, “B32”, “B33” } };

var list = new List<A> { a1, a2, a3 };

 

we can do the following:

var result =

    from a in list

    let id = a.Id

    from b in a.Collection

    select Tuple.Create(id, b);

 

foreach (var tuple in result)

{

    Console.WriteLine(“{0}, {1}”, tuple.Item1, tuple.Item2);

}

 

which will output:

1, B11
1, B12
1, B13
2, B21
2, B22
2, B23
3, B31
3, B32
3, B33

Categories: Uncategorized

Powershell, advanced functions

November 11, 2010 Leave a comment

According to MSDN advanced functions (or script cmdlets) are defined as

Advanced functions allow you to write functions that can perform operations 
that are similar to the operations you can perform with cmdlets. Advanced 
functions are helpful when you want to quickly write a function without 
having to write a compiled cmdlet using a Microsoft .NET Framework 
language. These functions are also helpful when you want to restrict the
functionality of a compiled cmdlet or when you want to write a function 
that is similar to a compiled cmdlet.

    Advanced functions are defined using the CmdletBinding attribute. The attribute has four public properties:

    • ConfirmImpact
    • SupportsShouldProcess
    • SupportsTransactions
    • DefaultParameterSetName

    I think that, in most cases, the defaults are used and in this case an advanced function declaration might look like:

    function Increment()
    {
        [CmdletBinding()]
        param([Parameter(Position=0, Mandatory=$True)]$value)
        return $value + 1
    }

    This function accepts a mandatory parameter called value, which can either be specified by name:

    Increment -Value 10

    Or by position (0):

    Increment 10

    Okay, that was simple. Lets extend our increment function:

    function Increment()
    {
        [CmdletBinding()]
        param(
            [Parameter(
                Position=0,
                Mandatory=$True,
                ValueFromPipeline=$True,
                ValueFromPipelineByPropertyName=$True)]
            [int[]]$value,
            [Parameter(
                Position=1,
                Mandatory=$False,
                ValueFromPipelineByPropertyName=$True)]
            [Alias(‘delta’)]
            [int]$step = 1
            )

        begin
        {
            # $psBoundParameters is a hashtable containing the parameters
            # that were bound and what values were bound to them. 
            $inputFromPipeline = !$psBoundParameters.containskey(‘value’)
        }

        process
        {
            if ($inputFromPipeline)
            {
                $value[0] + $signedStep
            }
            else
            {
                $res = @()
                foreach($v in $value)
                {
                    $res += $v + $signedStep
                }
                $res
            }
        }

        end
        {
        }
    }

    This function can be called in a lot of different ways:

    # Sample usage: Positional argument
    Increment 10 2
    Increment 10,20,30 2

    # Sample usage: Named argument
    Increment -Value 10 -Step 2
    Increment -Value 10,20,30 -Step 2

    # Sample usage: Default values
    Increment -Value 10

    # Sample usage: Aliased argument names
    Increment -Value 10 -Delta 2

    # Sample usage: Value from pipeline
    10 | Increment -Step 2
    10,20,30 | Increment -Step 2

    # Sample usage: Value from pipeline by property name
    $obj = new-object psobject -property @{ Value = 13; Step=2 }
    $obj | Increment

    Almost overwhelming… but powerful (of course).

    Categories: Uncategorized

    Converting Autocad DWG files to ESRI Shapefiles

    November 1, 2010 1 comment

    There are plenty of tools that can convert DWG files to Shape files (FME, CAD2Shape etc) but most of them are not free. There are however, ways to do this for free – even though it might not be as convenient. One way is to use ODA Teigha File Converter to convert to an intermediate DXF file and then use OGR in FWTools to convert that into shape. I tested this approach using a sample file, gaston14.dwg, download from Geodetic Survey.

    Alright, let’s see some code. Launching ODA Teigha File Converter  (EveryDWG.exe) from the command line can be accomplished by:

    EveryDWG.exe C:\…\input C:\…\output ACAD2007 DXF 0 1

    This assumes that input and output are folders that exists and that the input folder contains the dwg file to be converted which in this case is gaston14.dwg. Not that pretty, but it works. Now the output will contain a file called gaston14.dxf and we can use ogr2ogr to convert this into shape files. Since shape files can only one feature type we will create one for points, one for lines and one for polygons:

    ogr2ogr gaston14_points.shp gaston14.dxf -where "ogr_geometry = ‘POINT’"
    ogr2ogr gaston14_lines.shp gaston14.dxf -where "ogr_geometry = ‘LINESTRING’"
    ogr2ogr gaston14_polygons.shp gaston14.dxf -where "ogr_geometry = ‘POLYGON’"

    This assumes that current directory is the output folder.

    Categories: Uncategorized