Subscribe to this thread
Home - General / All posts - Copy shape file to sql server using .Net API
nityaj4 post(s)
#08-May-19 16:16

I'm writing a .net web application to copy a shape file to a sql server.

I read the documentation and understand how to create a datasource to sql server and the shape file.

I couldn't find methods or samples in the API documentation to copy or import the shape file to sql server.

Is it also possible to achieve the same thing with geodatabase files?

Please help.

Thanks.

adamw


8,584 post(s)
#09-May-19 11:53

Here is example code:

// C#

 

class Script

{

 

// get all schema fields

static string[] GetSchemaFields(Manifold.Schema schema)

{

  int count = schema.Fields.Count;

  string[] fields = new string[count];

  for (int index = 0; index < count; ++index)

    fields[index] = schema.Fields[index].Name;

  return fields;

}

 

static Manifold.Context Manifold;

static void Main()

{

  Manifold.Database rootDb = Manifold.Application.GetDatabaseRoot();

  Manifold.Database srcDb = rootDb.GetChild("SHP"truetrue);

  Manifold.Database tgtDb = rootDb.GetChild("SQLServer"truetrue);

 

  // find original table

  Manifold.Table src = srcDb.Search("Example Table");

 

  // create copy of table with same fields and indexes

  Manifold.Schema srcSchema = src.GetSchema();

  string tgtName = tgtDb.Insert("Example Table""table", srcSchema);

  Manifold.Table tgt = tgtDb.Search(tgtName);

 

  // copy records

  Manifold.Sequence sequence = src.SearchAll(GetSchemaFields(srcSchema));

  if (sequence != null)

    tgt.InsertBatch(sequence);

}

 

}

...this copies a table named 'Example Table' from the data source named 'SHP' to the data source named 'SQLServer'. If the latter data source ends up being a database, it will also create a virtual drawing for each geometry field.

You probably also want to copy the coordinate system - since SQL Server is a database, we have to use a database-specific coordinate system: 'SRID:xxx', but for SQL Server specifically, 'xxx' is an EPSG code. To set the coordinate system of a geometry field, pass necessary properties to the call to Database.Insert - you can check what properties tables have in the UI.

The code above also lacks calls to using(...) - that does not create any leaks because everything is cleaned up after the script completes, but if you have a script that copies hundreds of tables, you might want to clean things up earlier explicitly.

Hope this helps.

nityaj4 post(s)
#09-May-19 16:38

Hello, thank you very much for replying.

I'm trying your example.

The SearchAll for all fields from the Schema in shape file returns only the geometry field. In the UI, when I open the Schema, I can see all the fields, but not in the API.

Here's the shape file:

http://dallascad.org/ViewPDFs.aspx?type=3&id=\\DCAD.ORG\WEB\WEBDATA\WEBFORMS\GIS%20PRODUCTS\PARCEL.zip

adamw


8,584 post(s)
#10-May-19 17:01

I downloaded your file, linked it to a new MAP file as 'PARCEL', then checked what fields the source table - as seen by the script - contains:

// C#

 

class Script

{

 

// get all schema fields

static string[] GetSchemaFields(Manifold.Schema schema)

{

  int count = schema.Fields.Count;

  string[] fields = new string[count];

  for (int index = 0; index < count; ++index)

    fields[index] = schema.Fields[index].Name;

  return fields;

}

 

static Manifold.Context Manifold;

static void Main()

{

  Manifold.Database rootDb = Manifold.Application.GetDatabaseRoot();

  Manifold.Database srcDb = rootDb.GetChild("PARCEL"truetrue);

 

  // find original table

  Manifold.Table src = srcDb.Search("PARCEL Table");

 

  // dump table fields

  Manifold.Schema srcSchema = src.GetSchema();

  string[] fields = GetSchemaFields(srcSchema);

  for (int index = 0; index < fields.Length; ++index)

    Manifold.Application.Log(fields[index]);

  Manifold.Application.OpenLog();

}

 

}

...and after some waiting (spent building the RTREE index for the cache, I could have linked the file with the cache turned off to avoid that) got a list of all fields in the log:

2019-05-10 ...  ++ mfd_id

2019-05-10 ...  ++ Acct

2019-05-10 ...  ++ RecAcs

2019-05-10 ...  ++ Shape_area

2019-05-10 ...  ++ Shape_len

2019-05-10 ...  ++ Geom

Did I understand the problem correctly?

Manifold User Community Use Agreement Copyright (C) 2007-2017 Manifold Software Limited. All rights reserved.