2
2
using System . Collections . ObjectModel ;
3
3
using System . IO ;
4
4
using GeoAPI . Geometries ;
5
+ using NetTopologySuite . Algorithm . Match ;
5
6
using NetTopologySuite . Features ;
6
7
using NetTopologySuite . Geometries ;
8
+ using NetTopologySuite . Geometries . Implementation ;
7
9
using NetTopologySuite . IO ;
8
10
using NetTopologySuite . Samples . SimpleTests ;
9
11
using NUnit . Framework ;
@@ -16,11 +18,34 @@ public class ShapeFileDataWriterTest : BaseSamples
16
18
public ShapeFileDataWriterTest ( )
17
19
{
18
20
// Set current dir to shapefiles dir
19
- Environment . CurrentDirectory = Path . Combine (
20
- AppDomain . CurrentDomain . BaseDirectory ,
21
+ Environment . CurrentDirectory = Path . Combine (
22
+ AppDomain . CurrentDomain . BaseDirectory ,
21
23
string . Format ( "..{0}..{0}..{0}NetTopologySuite.Samples.Shapefiles" , Path . DirectorySeparatorChar ) ) ;
22
24
}
23
25
26
+ [ Test ]
27
+ public void TestCreateEmptyShapefile ( )
28
+ {
29
+ const string filename = "__empty" ;
30
+ const string emptyShp = filename + ".shp" ;
31
+ const string emptyShx = filename + ".shx" ;
32
+ const string emptyDbf = filename + ".dbf" ;
33
+ if ( File . Exists ( emptyShp ) )
34
+ File . Delete ( emptyShp ) ;
35
+ if ( File . Exists ( emptyShx ) )
36
+ File . Delete ( emptyShx ) ;
37
+ if ( File . Exists ( emptyDbf ) )
38
+ File . Delete ( emptyDbf ) ;
39
+
40
+ ShapefileDataWriter writer = new ShapefileDataWriter ( filename , Factory ) ;
41
+ writer . Header = new DbaseFileHeader ( ) ;
42
+ writer . Write ( new IFeature [ 0 ] ) ;
43
+
44
+ Assert . That ( File . Exists ( emptyShp ) , Is . True ) ;
45
+ Assert . That ( File . Exists ( emptyShx ) , Is . True ) ;
46
+ Assert . That ( File . Exists ( emptyDbf ) , Is . True ) ;
47
+ }
48
+
24
49
[ Test ]
25
50
public void TestWriteZValuesShapeFile ( )
26
51
{
@@ -39,14 +64,14 @@ private void TestWriteZMValuesShapeFile(bool testM)
39
64
points [ 1 ] = new Coordinate ( 1 , 0 ) ;
40
65
points [ 2 ] = new Coordinate ( 1 , 1 ) ;
41
66
42
- var csFactory = NetTopologySuite . Geometries . Implementation . DotSpatialAffineCoordinateSequenceFactory . Instance ;
67
+ var csFactory = DotSpatialAffineCoordinateSequenceFactory . Instance ;
43
68
var sequence = csFactory . Create ( 3 , Ordinates . XYZM ) ;
44
- for ( var i = 0 ; i < 3 ; i ++ )
69
+ for ( var i = 0 ; i < 3 ; i ++ )
45
70
{
46
71
sequence . SetOrdinate ( i , Ordinate . X , points [ i ] . X ) ;
47
72
sequence . SetOrdinate ( i , Ordinate . Y , points [ i ] . Y ) ;
48
73
sequence . SetOrdinate ( i , Ordinate . Z , 1 + i ) ;
49
- if ( testM )
74
+ if ( testM )
50
75
sequence . SetOrdinate ( i , Ordinate . M , 11 + i ) ;
51
76
}
52
77
var lineString = Factory . CreateLineString ( sequence ) ;
@@ -65,7 +90,7 @@ private void TestWriteZMValuesShapeFile(bool testM)
65
90
shpWriter . Write ( features ) ;
66
91
67
92
// Now let's read the file and verify that we got Z and M back
68
- var factory = new GeometryFactory ( NetTopologySuite . Geometries . Implementation . DotSpatialAffineCoordinateSequenceFactory . Instance ) ;
93
+ var factory = new GeometryFactory ( DotSpatialAffineCoordinateSequenceFactory . Instance ) ;
69
94
70
95
using ( var reader = new ShapefileDataReader ( "ZMtest" , factory ) )
71
96
{
@@ -75,12 +100,12 @@ private void TestWriteZMValuesShapeFile(bool testM)
75
100
for ( var i = 0 ; i < 3 ; i ++ )
76
101
{
77
102
var c = geom . Coordinates [ i ] ;
78
- Assert . AreEqual ( i + 1 , c . Z ) ;
103
+ Assert . AreEqual ( i + 1 , c . Z ) ;
79
104
}
80
105
81
106
if ( testM )
82
107
{
83
- sequence = ( ( ILineString ) geom ) . CoordinateSequence ;
108
+ sequence = ( ( ILineString ) geom ) . CoordinateSequence ;
84
109
for ( var i = 0 ; i < 3 ; i ++ )
85
110
{
86
111
Assert . AreEqual ( sequence . GetOrdinate ( i , Ordinate . M ) , 11 + i ) ;
@@ -91,7 +116,7 @@ private void TestWriteZMValuesShapeFile(bool testM)
91
116
var v = reader . GetString ( 0 ) ;
92
117
Assert . AreEqual ( v , "Trond" ) ;
93
118
}
94
- }
119
+ }
95
120
96
121
[ Test ]
97
122
public void TestWriteSimpleShapeFile ( )
@@ -101,7 +126,7 @@ public void TestWriteSimpleShapeFile()
101
126
102
127
var coll = new GeometryCollection ( new IGeometry [ ] { p1 , p2 , } ) ;
103
128
ShapefileWriter . WriteGeometryCollection ( @"test_arcview" , coll ) ;
104
-
129
+
105
130
// Not read by ArcView!!!
106
131
}
107
132
@@ -191,9 +216,9 @@ private static void DoTest(IGeometryCollection geomsWrite, Ordinates ordinates,
191
216
}
192
217
else if ( ! gw . EqualsExact ( gr ) )
193
218
{
194
- var hsm = new Algorithm . Match . HausdorffSimilarityMeasure ( ) . Measure ( gw , gr ) ;
195
- var asm = new Algorithm . Match . AreaSimilarityMeasure ( ) . Measure ( gw , gr ) ;
196
- var smc = Algorithm . Match . SimilarityMeasureCombiner . Combine ( hsm , asm ) ;
219
+ var hsm = new HausdorffSimilarityMeasure ( ) . Measure ( gw , gr ) ;
220
+ var asm = new AreaSimilarityMeasure ( ) . Measure ( gw , gr ) ;
221
+ var smc = SimilarityMeasureCombiner . Combine ( hsm , asm ) ;
197
222
if ( ! gw . EqualsNormalized ( gr ) || ( 1d - smc ) > 1e-7 )
198
223
{
199
224
Console . WriteLine ( string . Format ( "Geometries don't match at index {0}" , i ) ) ;
@@ -254,7 +279,7 @@ private static bool ArraysEqual(double[] writeZ, double[] readZ)
254
279
255
280
for ( var i = 0 ; i < writeZ . Length ; i ++ )
256
281
if ( Math . Abs ( writeZ [ i ] - readZ [ i ] ) > 1E-7 ) return false ;
257
-
282
+
258
283
return true ;
259
284
}
260
285
@@ -263,8 +288,8 @@ private static class ShapeFileShapeFactory
263
288
public static IGeometryCollection CreateShapes ( OgcGeometryType type , Ordinates ordinates , int number = 50 )
264
289
{
265
290
var empty = new bool [ number ] ;
266
- empty [ Rnd . Next ( 2 , number / 2 ) ] = true ;
267
- empty [ Rnd . Next ( number / 2 , number ) ] = true ;
291
+ empty [ Rnd . Next ( 2 , number / 2 ) ] = true ;
292
+ empty [ Rnd . Next ( number / 2 , number ) ] = true ;
268
293
269
294
var result = new IGeometry [ number ] ;
270
295
for ( var i = 0 ; i < number ; i ++ )
@@ -300,14 +325,14 @@ public static IGeometryCollection CreateShapes(OgcGeometryType type, Ordinates o
300
325
301
326
return Factory . CreateGeometryCollection ( result ) ;
302
327
}
303
-
328
+
304
329
private static readonly Random Rnd = new Random ( 9936528 ) ;
305
330
306
331
private static readonly ICoordinateSequenceFactory CsFactory =
307
- NetTopologySuite . Geometries . Implementation . DotSpatialAffineCoordinateSequenceFactory . Instance ;
332
+ DotSpatialAffineCoordinateSequenceFactory . Instance ;
308
333
309
334
public static readonly IGeometryFactory FactoryRead = new GeometryFactory ( new PrecisionModel ( PrecisionModels . Floating ) , 4326 , CsFactory ) ;
310
-
335
+
311
336
public static readonly IGeometryFactory Factory = new GeometryFactory ( new PrecisionModel ( 1000 ) , 4326 , CsFactory ) ;
312
337
313
338
private static IGeometry CreatePoint ( Ordinates ordinates , bool empty )
@@ -334,7 +359,7 @@ private static IGeometry CreateMultiPoint(Ordinates ordinates, bool empty)
334
359
for ( var i = 0 ; i < numPoints ; i ++ )
335
360
foreach ( var o in OrdinatesUtility . ToOrdinateArray ( ordinates ) )
336
361
seq . SetOrdinate ( i , o , RandomOrdinate ( o , Factory . PrecisionModel ) ) ;
337
-
362
+
338
363
return Factory . CreateMultiPoint ( seq ) ;
339
364
}
340
365
@@ -371,7 +396,7 @@ private static IGeometry CreateMultiLineString(Ordinates ordinates, bool empty)
371
396
{
372
397
Factory . CreateMultiLineString ( null ) ;
373
398
}
374
-
399
+
375
400
var numLineStrings = Rnd . Next ( 0 , 11 ) ;
376
401
if ( numLineStrings <= 2 )
377
402
numLineStrings = 0 ;
@@ -407,45 +432,45 @@ private static IGeometry CreatePolygon(Ordinates ordinates, bool empty, int next
407
432
var ring = CreateCircleRing ( ordinates , x , y , 3 * Rnd . NextDouble ( ) ) ;
408
433
return Factory . CreatePolygon ( ring , null ) ;
409
434
case 1 : // rectangle
410
- ring = CreateRectangleRing ( ordinates , x , y , 6 * Rnd . NextDouble ( ) , 3 * Rnd . NextDouble ( ) ) ;
435
+ ring = CreateRectangleRing ( ordinates , x , y , 6 * Rnd . NextDouble ( ) , 3 * Rnd . NextDouble ( ) ) ;
411
436
return Factory . CreatePolygon ( ring , null ) ;
412
437
case 2 : // cirle with hole
413
- var radius = 3 * Rnd . NextDouble ( ) ;
438
+ var radius = 3 * Rnd . NextDouble ( ) ;
414
439
var shell = CreateCircleRing ( ordinates , x , y , radius ) ;
415
440
var hole = CreateCircleRing ( ordinates , x , y , 0.66 * radius , true ) ;
416
- return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
441
+ return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
417
442
case 3 : // rectanglee with hole
418
- var width = 6 * Rnd . NextDouble ( ) ;
419
- var height = 3 * Rnd . NextDouble ( ) ;
443
+ var width = 6 * Rnd . NextDouble ( ) ;
444
+ var height = 3 * Rnd . NextDouble ( ) ;
420
445
shell = CreateRectangleRing ( ordinates , x , y , width , height ) ;
421
446
hole = CreateRectangleRing ( ordinates , x , y , 0.66 * width , 0.66 * height , true ) ;
422
- return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
447
+ return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
423
448
case 4 : // rectanglee with hole
424
- width = 6 * Rnd . NextDouble ( ) ;
425
- height = 3 * Rnd . NextDouble ( ) ;
449
+ width = 6 * Rnd . NextDouble ( ) ;
450
+ height = 3 * Rnd . NextDouble ( ) ;
426
451
shell = CreateRectangleRing ( ordinates , x , y , width , height ) ;
427
452
hole = CreateCircleRing ( ordinates , x , y , 0.33 * Math . Min ( width , height ) , true ) ;
428
- return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
453
+ return Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
429
454
default :
430
455
throw new NotSupportedException ( ) ;
431
456
}
432
457
}
433
458
434
459
private static ILinearRing CreateCircleRing ( Ordinates ordinates , double x , double y , double radius , bool reverse = false )
435
460
{
436
- var seq = CsFactory . Create ( 4 * 12 + 1 , ordinates ) ;
461
+ var seq = CsFactory . Create ( 4 * 12 + 1 , ordinates ) ;
437
462
var angle = Math . PI * 2 ;
438
- const double quandrantStep = Math . PI / 2d / 12d ;
463
+ const double quandrantStep = Math . PI / 2d / 12d ;
439
464
var k = 0 ;
440
465
for ( var i = 0 ; i < 4 ; i ++ )
441
466
{
442
467
for ( var j = 0 ; j < 12 ; j ++ )
443
468
{
444
- var dx = radius * Math . Cos ( angle ) ;
445
- var dy = radius * Math . Sin ( angle ) ;
469
+ var dx = radius * Math . Cos ( angle ) ;
470
+ var dy = radius * Math . Sin ( angle ) ;
446
471
seq . SetOrdinate ( k , Ordinate . X , Factory . PrecisionModel . MakePrecise ( x + dx ) ) ;
447
472
seq . SetOrdinate ( k , Ordinate . Y , Factory . PrecisionModel . MakePrecise ( y + dy ) ) ;
448
- if ( ( ordinates & Ordinates . Z ) == Ordinates . Z )
473
+ if ( ( ordinates & Ordinates . Z ) == Ordinates . Z )
449
474
seq . SetOrdinate ( k , Ordinate . Z , RandomOrdinate ( Ordinate . Z , Factory . PrecisionModel ) ) ;
450
475
if ( ( ordinates & Ordinates . Z ) == Ordinates . Z )
451
476
seq . SetOrdinate ( k , Ordinate . M , RandomOrdinate ( Ordinate . M , Factory . PrecisionModel ) ) ;
@@ -465,8 +490,8 @@ private static ILinearRing CreateCircleRing(Ordinates ordinates, double x, doubl
465
490
466
491
private static ILinearRing CreateRectangleRing ( Ordinates ordinates , double x , double y , double width , double height , bool reverse = false )
467
492
{
468
- var dx = Factory . PrecisionModel . MakePrecise ( width / 2 ) ;
469
- var dy = Factory . PrecisionModel . MakePrecise ( height / 2 ) ;
493
+ var dx = Factory . PrecisionModel . MakePrecise ( width / 2 ) ;
494
+ var dy = Factory . PrecisionModel . MakePrecise ( height / 2 ) ;
470
495
471
496
var seq = CsFactory . Create ( 5 , ordinates ) ;
472
497
@@ -499,14 +524,14 @@ private static ILinearRing CreateRectangleRing(Ordinates ordinates, double x, do
499
524
500
525
return Factory . CreateLinearRing ( reverse ? seq . Reversed ( ) : seq ) ;
501
526
}
502
-
527
+
503
528
private static IGeometry CreateMultiPolygon ( Ordinates ordinates , bool empty )
504
529
{
505
530
if ( empty )
506
531
{
507
532
Factory . CreateMultiPolygon ( null ) ;
508
533
}
509
-
534
+
510
535
switch ( Rnd . Next ( 2 ) )
511
536
{
512
537
case 0 :
@@ -515,15 +540,15 @@ private static IGeometry CreateMultiPolygon(Ordinates ordinates, bool empty)
515
540
for ( var i = 0 ; i < numPolygons ; i ++ )
516
541
polygons [ i ] = ( IPolygon ) CreatePolygon ( ordinates , false ) ;
517
542
return Factory . BuildGeometry ( new Collection < IGeometry > ( polygons ) ) . Union ( ) ;
518
-
543
+
519
544
case 1 :
520
545
polygons = new IPolygon [ 2 ] ;
521
- var radius = 5 * Rnd . NextDouble ( ) ;
546
+ var radius = 5 * Rnd . NextDouble ( ) ;
522
547
var x = RandomOrdinate ( Ordinate . X , Factory . PrecisionModel ) ;
523
548
var y = RandomOrdinate ( Ordinate . Y , Factory . PrecisionModel ) ;
524
549
var shell = CreateCircleRing ( ordinates , x , y , radius ) ;
525
550
var hole = CreateCircleRing ( ordinates , x , y , 0.66 * radius , true ) ;
526
- polygons [ 0 ] = Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
551
+ polygons [ 0 ] = Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
527
552
shell = CreateCircleRing ( ordinates , x , y , 0.5 * radius ) ;
528
553
hole = CreateCircleRing ( ordinates , x , y , 0.15 * radius , true ) ;
529
554
polygons [ 1 ] = Factory . CreatePolygon ( shell , new [ ] { hole } ) ;
@@ -539,7 +564,7 @@ private static double RandomOrdinate(Ordinate o, IPrecisionModel pm)
539
564
switch ( o )
540
565
{
541
566
case Ordinate . X :
542
- return pm . MakePrecise ( - 180 + 360 * Rnd . NextDouble ( ) ) ;
567
+ return pm . MakePrecise ( - 180 + 360 * Rnd . NextDouble ( ) ) ;
543
568
case Ordinate . Y :
544
569
return pm . MakePrecise ( - 90 + 180 * Rnd . NextDouble ( ) ) ;
545
570
case Ordinate . Z :
@@ -551,7 +576,7 @@ private static double RandomOrdinate(Ordinate o, IPrecisionModel pm)
551
576
}
552
577
}
553
578
}
554
-
579
+
555
580
[ Test , ExpectedException ( typeof ( ArgumentException ) ) ]
556
581
// see https://code.google.com/p/nettopologysuite/issues/detail?id=146
557
582
public void Issue146_ShapeCreationWithInvalidAttributeName ( )
0 commit comments