shail_GIS
unread,Nov 9, 2008, 4:38:05 PM11/9/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to NetTopologySuite
This is a greatest library, if it goes along well with sharpmap.
I prefer this one over shapelib, since this seems to be more active
and great future as well, if we could make WCF, restfull service for
GeoJSON and JSON output of our shapes(shapefile, SQL Server 08).
I converted the vb to c# codes from TestShapeNTS.
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Deployment;
namespace TestShapeNtscNet
{
class Test
{
/*
static void Main(string[] args)
{
}
*/
private GisSharpBlog.NetTopologySuite.Geometries.CoordinateList
m_coordinates;
private const string savePath = "c:\\Temp\\ShapeTest\\Output\
\Test2\\";
public static void Main()
{
Test instance = new Test();
instance.run();
}
private Test()
{
m_coordinates = new
GisSharpBlog.NetTopologySuite.Geometries.CoordinateList();
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488125.37,
5271612.62));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488137.82,
5271614.88));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488150.64,
5271617.52));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488178.92,
5271617.52));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488205.31,
5271614.13));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488229.06,
5271602.44));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488258.1,
5271592.26));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488277.33,
5271587.36));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488306.36,
5271582.08));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488336.53,
5271579.82));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488363.3,
5271583.59));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488378.76,
5271587.36));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488398.37,
5271595.65));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488436.19,
5271612.98));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488437.2,
5271606.59));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488428.15,
5271593.01));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488430.42,
5271584.34));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488418.35,
5271579.82));
m_coordinates.Add(new
GisSharpBlog.NetTopologySuite.Geometries.Coordinate(4488414.87,
5271570.84));
}
public void run()
{
GisSharpBlog.NetTopologySuite.IO.ShapefileDataWriter
shapeDataWriter =
default(GisSharpBlog.NetTopologySuite.IO.ShapefileDataWriter);
GisSharpBlog.NetTopologySuite.IO.DbaseFileHeader header =
default(GisSharpBlog.NetTopologySuite.IO.DbaseFileHeader);
System.IO.DirectoryInfo outputDirectory =
default(System.IO.DirectoryInfo);
outputDirectory = new System.IO.DirectoryInfo(savePath);
if (System.IO.Directory.Exists(savePath)) {
foreach (System.IO.FileInfo oldFile in
outputDirectory.GetFiles())
{
//string Tests = "";
//System.IO.FileInfo oldFile.Delete();
oldFile.Delete();
}
}
else {
System.IO.Directory.CreateDirectory(savePath);
}
Console.WriteLine("Test started...");
try {
header = createHeader();
shapeDataWriter = new
GisSharpBlog.NetTopologySuite.IO.ShapefileDataWriter(savePath +
"Test");
shapeDataWriter.Header = header;
GisSharpBlog.NetTopologySuite.Features.Feature[]
ntsFeatures = null;
ntsFeatures = createNtsFeatures();
shapeDataWriter.Write(ntsFeatures);
Console.WriteLine("Test ended..." + ntsFeatures.Length +
" features written");
}
catch (Exception ex) {
Console.WriteLine("Error!!! " + ex.Message);
}
Console.WriteLine("Press [Enter] to quit");
Console.ReadLine();
}
private GisSharpBlog.NetTopologySuite.IO.DbaseFileHeader
createHeader()
{
GisSharpBlog.NetTopologySuite.IO.DbaseFileHeader returnValue
= null;
try {
returnValue = new
GisSharpBlog.NetTopologySuite.IO.DbaseFileHeader();
Char testchar = Convert.ToChar("C");
returnValue.AddColumn("Field1", testchar, 80, 0);
returnValue.AddColumn("Field2", testchar, 80, 0);
returnValue.AddColumn("Field3", testchar, 80, 0);
returnValue.AddColumn("Field4", testchar, 80, 0);
//returnValue.NumRecords = 19;
}
catch (Exception ex) {
Console.WriteLine("Error in createHeader:" + ex.Message);
}
return returnValue;
}
private GisSharpBlog.NetTopologySuite.Features.Feature[]
createNtsFeatures()
{
List<GisSharpBlog.NetTopologySuite.Features.Feature>
returnValue = new
List<GisSharpBlog.NetTopologySuite.Features.Feature>();
try {
foreach
(GisSharpBlog.NetTopologySuite.Geometries.Coordinate coordinate in
m_coordinates) {
GisSharpBlog.NetTopologySuite.Features.Feature
ntsFeature = default(GisSharpBlog.NetTopologySuite.Features.Feature);
ntsFeature = new
GisSharpBlog.NetTopologySuite.Features.Feature();
//Geometrie
GisSharpBlog.NetTopologySuite.Geometries.Geometry
geometry = null;
try {
geometry =
this.createNtsGeometryPoint(coordinate);
}
catch (Exception ex) {
Console.WriteLine("Error!!! " + ex.Message);
}
//Attributes
GisSharpBlog.NetTopologySuite.Features.AttributesTable ntsAttributes =
new GisSharpBlog.NetTopologySuite.Features.AttributesTable();
ntsAttributes.AddAttribute("Field1",
"nettopologysuite is groovy");
ntsAttributes.AddAttribute("Field2",
"nettopologysuite is funky");
ntsAttributes.AddAttribute("Field3",
"nettopologysuite is special");
ntsAttributes.AddAttribute("Field4",
"nettopologysuite works great");
ntsFeature.Attributes = ntsAttributes;
ntsFeature.Geometry = geometry;
returnValue.Add(ntsFeature);
}
}
catch (Exception ex) {
Console.WriteLine("Error!!! " + ex.Message);
}
return returnValue.ToArray();
}
private GisSharpBlog.NetTopologySuite.Geometries.Geometry
createNtsGeometryPoint(GisSharpBlog.NetTopologySuite.Geometries.Coordinate
coordinate)
{
GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory
factory = new
GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory();
GisSharpBlog.NetTopologySuite.Geometries.Geometry ntsGeometry
= null; ///null;
factory.CreatePoint(coordinate);
return ntsGeometry;
}
}
}
Originally it used to give me an error that cast is missing , it
stopped for some reason.
But now i am running into the following error.
ERROR!!! geometries must not contain null elements
I made this one a console application.
Please help...