Skip to content

Commit 9950042

Browse files
committed
Code cleanup
1 parent 5ee3444 commit 9950042

File tree

5 files changed

+75
-66
lines changed

5 files changed

+75
-66
lines changed

src/NetTopologySuite.IO.SpatiaLite/GaiaGeoWriter.cs

+53-43
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,30 @@ public Ordinates HandleOrdinates
5252
/// </summary>
5353
public bool UseCompressed { get; set; }
5454

55+
/// <summary>
56+
/// Writes the provided <paramref name="geometry"/> to the given <paramref name="stream"/>.
57+
/// </summary>
58+
/// <param name="geometry">A geometry</param>
59+
/// <param name="stream">The stream to write to</param>
5560
public void Write(Geometry geometry, Stream stream)
5661
{
57-
var g = Write(geometry);
62+
byte[] g = Write(geometry);
5863
stream.Write(g, 0, g.Length);
5964
}
6065

61-
public byte[] Write(Geometry geom)
66+
/// <summary>
67+
/// Writes the provided <paramref name="geometry"/> to an array of bytes
68+
/// </summary>
69+
/// <param name="geometry">A geometry</param>
70+
/// <returns>An array of bytes</returns>
71+
public byte[] Write(Geometry geometry)
6272
{
6373
//if (geom.IsEmpty)
6474
// return GaiaGeoEmptyHelper.EmptyGeometryCollectionWithSrid(geom.SRID);
6575

66-
var ordinates = CheckOrdinates(geom);
67-
var hasZ = (ordinates & Ordinates.Z) == Ordinates.Z;
68-
var hasM = (ordinates & Ordinates.M) == Ordinates.M;
76+
var ordinates = CheckOrdinates(geometry);
77+
bool hasZ = (ordinates & Ordinates.Z) == Ordinates.Z;
78+
bool hasM = (ordinates & Ordinates.M) == Ordinates.M;
6979

7080
var gaiaExport = SetGaiaGeoExportFunctions(GaiaGeoEndianMarker.GAIA_LITTLE_ENDIAN, hasZ, hasM, UseCompressed);
7181

@@ -77,10 +87,10 @@ public byte[] Write(Geometry geom)
7787
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_START);
7888
bw.Write((byte)GaiaGeoEndianMarker.GAIA_LITTLE_ENDIAN);
7989
//SRID
80-
gaiaExport.WriteInt32(bw, geom.SRID);
90+
gaiaExport.WriteInt32(bw, geometry.SRID);
8191
//MBR
82-
var env = geom.EnvelopeInternal; //.Coordinates;
83-
if (geom.IsEmpty)
92+
var env = geometry.EnvelopeInternal; //.Coordinates;
93+
if (geometry.IsEmpty)
8494
{
8595
gaiaExport.WriteDouble(bw, 0d, 0d, 0d, 0d);
8696
}
@@ -92,7 +102,7 @@ public byte[] Write(Geometry geom)
92102
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_MBR);
93103

94104
//Write geometry
95-
WriteGeometry(geom, gaiaExport, bw);
105+
WriteGeometry(geometry, gaiaExport, bw);
96106

97107
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_END);
98108
}
@@ -126,7 +136,7 @@ private static WriteCoordinates SetWriteCoordinatesFunction(GaiaExport gaiaExpor
126136

127137
private static void WriteGeometry(Geometry geom, GaiaExport gaiaExport, BinaryWriter bw)
128138
{
129-
WriteCoordinates writeCoordinates = SetWriteCoordinatesFunction(gaiaExport);
139+
var writeCoordinates = SetWriteCoordinatesFunction(gaiaExport);
130140

131141
//Geometry type
132142
int coordinateFlag = gaiaExport.CoordinateFlag;
@@ -173,7 +183,7 @@ private static void WriteGeometry(Geometry geom, GaiaExport gaiaExport, BinaryWr
173183
private static void WriteGeometryCollection(GeometryCollection geom, GaiaExport gaiaExport, BinaryWriter bw)
174184
{
175185
gaiaExport.WriteInt32(bw, geom.NumGeometries);
176-
for (var i = 0; i < geom.NumGeometries; i++)
186+
for (int i = 0; i < geom.NumGeometries; i++)
177187
{
178188
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
179189
WriteGeometry(geom[i], gaiaExport, bw);
@@ -183,7 +193,7 @@ private static void WriteGeometryCollection(GeometryCollection geom, GaiaExport
183193
private static void WriteMultiPolygon(GeometryCollection geom, WriteCoordinates writeCoordinates, GaiaExport gaiaExport, BinaryWriter bw)
184194
{
185195
gaiaExport.WriteInt32(bw, geom.NumGeometries);
186-
for (var i = 0; i < geom.NumGeometries; i++)
196+
for (int i = 0; i < geom.NumGeometries; i++)
187197
{
188198
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
189199
gaiaExport.WriteInt32(bw, gaiaExport.CoordinateFlag | (int)GaiaGeoGeometry.GAIA_POLYGON);
@@ -194,7 +204,7 @@ private static void WriteMultiPolygon(GeometryCollection geom, WriteCoordinates
194204
private static void WriteMultiLineString(MultiLineString geom, WriteCoordinates writeCoordinates, GaiaExport gaiaExport, BinaryWriter bw)
195205
{
196206
gaiaExport.WriteInt32(bw, geom.NumGeometries);
197-
for (var i = 0; i < geom.NumGeometries; i++)
207+
for (int i = 0; i < geom.NumGeometries; i++)
198208
{
199209
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
200210
gaiaExport.WriteInt32(bw, gaiaExport.CoordinateFlag | (int)GaiaGeoGeometry.GAIA_LINESTRING);
@@ -210,9 +220,9 @@ private static void WriteMultiPoint(MultiPoint geom, WriteCoordinates writeCoord
210220
wi(bw, geom.NumGeometries);
211221

212222
// get the coordinate flag
213-
var coordinateFlag = gaiaExport.CoordinateFlagUncompressed;
223+
int coordinateFlag = gaiaExport.CoordinateFlagUncompressed;
214224

215-
for (var i = 0; i < geom.NumGeometries; i++)
225+
for (int i = 0; i < geom.NumGeometries; i++)
216226
{
217227
//write entity begin marker
218228
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
@@ -229,7 +239,7 @@ private static void WritePolygon(Polygon geom, WriteCoordinates writeCoordinates
229239
{
230240
gaiaExport.WriteInt32(bw, geom.NumInteriorRings + 1);
231241
WriteLineString(geom.Shell, writeCoordinates, gaiaExport, bw);
232-
for (var i = 0; i < geom.NumInteriorRings; i++)
242+
for (int i = 0; i < geom.NumInteriorRings; i++)
233243
WriteLineString(geom.GetInteriorRingN(i), writeCoordinates, gaiaExport, bw);
234244
}
235245

@@ -247,7 +257,7 @@ private static void WritePoint(Point geom, WriteCoordinates writeCoordinates, Ga
247257

248258
private static GaiaExport SetGaiaGeoExportFunctions(GaiaGeoEndianMarker gaiaGeoEndianMarker, bool hasZ, bool hasM, bool useCompression)
249259
{
250-
var conversionNeeded = false;
260+
bool conversionNeeded = false;
251261
switch (gaiaGeoEndianMarker)
252262
{
253263
case GaiaGeoEndianMarker.GAIA_LITTLE_ENDIAN:
@@ -274,7 +284,7 @@ private static void WriteXY(CoordinateSequence coordinateSequence, GaiaExport ex
274284
{
275285
var wd = export.WriteDouble;
276286

277-
for (var i = 0; i < coordinateSequence.Count; i++)
287+
for (int i = 0; i < coordinateSequence.Count; i++)
278288
{
279289
var c = coordinateSequence.GetCoordinate(i);
280290
wd(bw, c.X, c.Y);
@@ -284,7 +294,7 @@ private static void WriteXY(CoordinateSequence coordinateSequence, GaiaExport ex
284294
private static void WriteXYZ(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
285295
{
286296
var wd = export.WriteDouble;
287-
for (var i = 0; i < coordinateSequence.Count; i++)
297+
for (int i = 0; i < coordinateSequence.Count; i++)
288298
{
289299
var c = coordinateSequence.GetCoordinate(i);
290300
wd(bw, c.X, c.Y, c.Z);
@@ -294,7 +304,7 @@ private static void WriteXYZ(CoordinateSequence coordinateSequence, GaiaExport e
294304
private static void WriteXYM(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
295305
{
296306
var wd = export.WriteDouble;
297-
for (var i = 0; i < coordinateSequence.Count; i++)
307+
for (int i = 0; i < coordinateSequence.Count; i++)
298308
{
299309
var c = coordinateSequence.GetCoordinate(i);
300310
wd(bw, c.X, c.Y, coordinateSequence.GetOrdinate(i, Ordinate.M));
@@ -304,7 +314,7 @@ private static void WriteXYM(CoordinateSequence coordinateSequence, GaiaExport e
304314
private static void WriteXYZM(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
305315
{
306316
var wd = export.WriteDouble;
307-
for (var i = 0; i < coordinateSequence.Count; i++)
317+
for (int i = 0; i < coordinateSequence.Count; i++)
308318
{
309319
var c = coordinateSequence.GetCoordinate(i);
310320
wd(bw, c.X, c.Y, c.Z, coordinateSequence.GetOrdinate(i, Ordinate.M));
@@ -320,14 +330,14 @@ private static void WriteCompressedXY(CoordinateSequence coordinateSequence, Gai
320330
wd(bw, cprev.X, cprev.Y);
321331

322332
var ws = export.WriteSingle;
323-
var maxIndex = coordinateSequence.Count - 1;
333+
int maxIndex = coordinateSequence.Count - 1;
324334
if (maxIndex <= 0) return;
325335

326-
for (var i = 1; i < maxIndex; i++)
336+
for (int i = 1; i < maxIndex; i++)
327337
{
328338
var c = coordinateSequence.GetCoordinate(i);
329-
var fx = (float)(c.X - cprev.X);
330-
var fy = (float)(c.Y - cprev.Y);
339+
float fx = (float)(c.X - cprev.X);
340+
float fy = (float)(c.Y - cprev.Y);
331341
ws(bw, fx, fy);
332342
cprev = c;
333343
}
@@ -345,16 +355,16 @@ private static void WriteCompressedXYZ(CoordinateSequence coordinateSequence, Ga
345355
var cprev = coordinateSequence.GetCoordinate(0);
346356
wd(bw, cprev.X, cprev.Y, cprev.Z);
347357

348-
var maxIndex = coordinateSequence.Count - 1;
358+
int maxIndex = coordinateSequence.Count - 1;
349359
if (maxIndex <= 0) return;
350360

351361
var ws = export.WriteSingle;
352-
for (var i = 1; i < maxIndex; i++)
362+
for (int i = 1; i < maxIndex; i++)
353363
{
354364
var c = coordinateSequence.GetCoordinate(i);
355-
var fx = (float)(c.X - cprev.X);
356-
var fy = (float)(c.Y - cprev.Y);
357-
var fz = (float)(c.Z - cprev.Z);
365+
float fx = (float)(c.X - cprev.X);
366+
float fy = (float)(c.Y - cprev.Y);
367+
float fz = (float)(c.Z - cprev.Z);
358368
ws(bw, fx, fy, fz);
359369
cprev = c;
360370
}
@@ -368,19 +378,19 @@ private static void WriteCompressedXYM(CoordinateSequence coordinateSequence, Ga
368378

369379
// Write initial coordinate
370380
var cprev = coordinateSequence.GetCoordinate(0);
371-
var mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
381+
double mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
372382
wd(bw, cprev.X, cprev.Y, mprev);
373383

374-
var maxIndex = coordinateSequence.Count - 1;
384+
int maxIndex = coordinateSequence.Count - 1;
375385
if (maxIndex <= 0) return;
376386

377387
var ws = export.WriteSingle;
378-
for (var i = 1; i < maxIndex; i++)
388+
for (int i = 1; i < maxIndex; i++)
379389
{
380390
var c = coordinateSequence.GetCoordinate(i);
381-
var fx = (float)(c.X - cprev.X);
382-
var fy = (float)(c.Y - cprev.Y);
383-
var fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
391+
float fx = (float)(c.X - cprev.X);
392+
float fy = (float)(c.Y - cprev.Y);
393+
float fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
384394
ws(bw, fx, fy, fm);
385395
cprev = c;
386396
}
@@ -395,20 +405,20 @@ private static void WriteCompressedXYZM(CoordinateSequence coordinateSequence, G
395405

396406
// Write initial coordinate
397407
var cprev = coordinateSequence.GetCoordinate(0);
398-
var mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
408+
double mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
399409
wd(bw, cprev.X, cprev.Y, cprev.Z, mprev);
400410

401-
var maxIndex = coordinateSequence.Count - 1;
411+
int maxIndex = coordinateSequence.Count - 1;
402412
if (maxIndex <= 0) return;
403413

404414
var ws = export.WriteSingle;
405-
for (var i = 1; i < maxIndex; i++)
415+
for (int i = 1; i < maxIndex; i++)
406416
{
407417
var c = coordinateSequence.GetCoordinate(i);
408-
var fx = (float)(c.X - cprev.X);
409-
var fy = (float)(c.Y - cprev.Y);
410-
var fz = (float)(c.Z - cprev.Z);
411-
var fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
418+
float fx = (float)(c.X - cprev.X);
419+
float fy = (float)(c.Y - cprev.Y);
420+
float fz = (float)(c.Z - cprev.Z);
421+
float fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
412422
ws(bw, fx, fy, fz, fm);
413423
cprev = c;
414424
}

test/NetTopologySuite.IO.SpatiaLite.Test/AbstractIOFixture.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ protected set
113113
throw new InvalidOperationException();
114114

115115
var oldPM = RandomGeometryHelper.Factory.PrecisionModel;
116-
RandomGeometryHelper.Factory = RandomGeometryHelper.Factory is OgcCompliantGeometryFactory
117-
? new OgcCompliantGeometryFactory(oldPM, value)
116+
RandomGeometryHelper.Factory = RandomGeometryHelper.Factory is GeometryFactoryEx
117+
? new GeometryFactoryEx(oldPM, value)
118+
{ OrientationOfExteriorRing = LinearRingOrientation.CCW }
118119
: new GeometryFactory(oldPM, value);
119120
}
120121
}
@@ -139,8 +140,9 @@ protected set
139140
? factory.CoordinateSequenceFactory
140141
: CoordinateArraySequenceFactory.Instance;
141142

142-
if (RandomGeometryHelper.Factory is OgcCompliantGeometryFactory)
143-
RandomGeometryHelper.Factory = new OgcCompliantGeometryFactory(value, oldSrid, oldFactory);
143+
if (RandomGeometryHelper.Factory is GeometryFactoryEx)
144+
RandomGeometryHelper.Factory = new GeometryFactoryEx(value, oldSrid, oldFactory)
145+
{OrientationOfExteriorRing = LinearRingOrientation.CCW };
144146
else
145147
RandomGeometryHelper.Factory = new GeometryFactory(value, oldSrid, oldFactory);
146148
}
@@ -198,7 +200,7 @@ public Ordinates ClipOrdinates
198200

199201
public void PerformTest(Geometry gIn)
200202
{
201-
WKTWriter writer = new WKTWriter(4) { MaxCoordinatesPerLine = 3, };
203+
var writer = new WKTWriter(4) { MaxCoordinatesPerLine = 3, };
202204
byte[] b = null;
203205
Assert.DoesNotThrow(() => b = Write(gIn), "Threw exception during write:\n{0}", writer.WriteFormatted(gIn));
204206

test/NetTopologySuite.IO.SpatiaLite.Test/GeoPackageFixture.cs

+11-14
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,25 @@ protected override void CreateTestStore()
4141
if (File.Exists(Name))
4242
File.Delete(Name);
4343

44-
using (var conn = new SQLiteConnection($"Data Source=\"{Name}\""))
45-
{
46-
conn.Open();
47-
conn.EnableExtensions(true);
48-
SpatialiteLoader.Load(conn);
49-
using (var cmd = conn.CreateCommand())
50-
{
51-
cmd.CommandText =
52-
"CREATE TABLE \"nts_io_geopackage\" (id int primary key, wkt text, the_geom blob);";
53-
cmd.ExecuteNonQuery();
54-
}
55-
}
44+
using var conn = new SQLiteConnection("Data Source=\"" + Name + "\"");
45+
conn.Open();
46+
conn.EnableExtensions(true);
47+
SpatialiteLoader.Load(conn);
48+
49+
using var cmd = conn.CreateCommand();
50+
cmd.CommandText =
51+
"CREATE TABLE \"nts_io_geopackage\" (\"id\" INTEGER PRIMARY KEY, \"wkt\" TEXT, \"the_geom\" BLOB);";
52+
cmd.ExecuteNonQuery();
5653
}
5754

5855
protected override void CheckEquality(Geometry gIn, Geometry gParsed, WKTWriter writer)
5956
{
60-
var res = gIn.EqualsExact(gParsed);
57+
bool res = gIn.EqualsExact(gParsed);
6158
if (res) return;
6259

6360
if (Compressed)
6461
{
65-
var discreteHausdorffDistance =
62+
double discreteHausdorffDistance =
6663
Algorithm.Distance.DiscreteHausdorffDistance.Distance(gIn, gParsed);
6764
if (discreteHausdorffDistance > 0.05)
6865
{

test/NetTopologySuite.IO.SpatiaLite.Test/NetTopologySuite.IO.SpatiaLite.Test.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ItemGroup>
88
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
99
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
10-
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.7" />
10+
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
1111
<PackageReference Include="mod_spatialite" Version="4.3.0.1" />
1212
</ItemGroup>
1313

test/NetTopologySuite.IO.SpatiaLite.Test/SpatiaLiteFixture.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ protected override void CreateTestStore()
4747

4848
using var cmd = conn.CreateCommand();
4949
cmd.CommandText =
50-
"CREATE TABLE \"nts_io_spatialite\" (id int primary key, the_geom geometry);";
50+
"CREATE TABLE \"nts_io_spatialite\" (\"id\" INTEGER PRIMARY KEY, \"the_geom\" BLOB);";
5151
cmd.ExecuteNonQuery();
5252
}
5353

5454
protected override void CheckEquality(Geometry gIn, Geometry gParsed, WKTWriter writer)
5555
{
56-
var res = gIn.EqualsExact(gParsed);
56+
bool res = gIn.EqualsExact(gParsed);
5757
if (res) return;
5858

5959
if (Compressed)
6060
{
61-
var discreteHausdorffDistance =
61+
double discreteHausdorffDistance =
6262
Algorithm.Distance.DiscreteHausdorffDistance.Distance(gIn, gParsed);
6363
if (discreteHausdorffDistance > 0.05)
6464
{

0 commit comments

Comments
 (0)