Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FObermaier committed Nov 7, 2024
1 parent 5ee3444 commit 9950042
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 66 deletions.
96 changes: 53 additions & 43 deletions src/NetTopologySuite.IO.SpatiaLite/GaiaGeoWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,30 @@ public Ordinates HandleOrdinates
/// </summary>
public bool UseCompressed { get; set; }

/// <summary>
/// Writes the provided <paramref name="geometry"/> to the given <paramref name="stream"/>.
/// </summary>
/// <param name="geometry">A geometry</param>
/// <param name="stream">The stream to write to</param>
public void Write(Geometry geometry, Stream stream)
{
var g = Write(geometry);
byte[] g = Write(geometry);
stream.Write(g, 0, g.Length);
}

public byte[] Write(Geometry geom)
/// <summary>
/// Writes the provided <paramref name="geometry"/> to an array of bytes
/// </summary>
/// <param name="geometry">A geometry</param>
/// <returns>An array of bytes</returns>
public byte[] Write(Geometry geometry)
{
//if (geom.IsEmpty)
// return GaiaGeoEmptyHelper.EmptyGeometryCollectionWithSrid(geom.SRID);

var ordinates = CheckOrdinates(geom);
var hasZ = (ordinates & Ordinates.Z) == Ordinates.Z;
var hasM = (ordinates & Ordinates.M) == Ordinates.M;
var ordinates = CheckOrdinates(geometry);
bool hasZ = (ordinates & Ordinates.Z) == Ordinates.Z;
bool hasM = (ordinates & Ordinates.M) == Ordinates.M;

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

Expand All @@ -77,10 +87,10 @@ public byte[] Write(Geometry geom)
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_START);
bw.Write((byte)GaiaGeoEndianMarker.GAIA_LITTLE_ENDIAN);
//SRID
gaiaExport.WriteInt32(bw, geom.SRID);
gaiaExport.WriteInt32(bw, geometry.SRID);
//MBR
var env = geom.EnvelopeInternal; //.Coordinates;
if (geom.IsEmpty)
var env = geometry.EnvelopeInternal; //.Coordinates;
if (geometry.IsEmpty)
{
gaiaExport.WriteDouble(bw, 0d, 0d, 0d, 0d);
}
Expand All @@ -92,7 +102,7 @@ public byte[] Write(Geometry geom)
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_MBR);

//Write geometry
WriteGeometry(geom, gaiaExport, bw);
WriteGeometry(geometry, gaiaExport, bw);

bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_END);
}
Expand Down Expand Up @@ -126,7 +136,7 @@ private static WriteCoordinates SetWriteCoordinatesFunction(GaiaExport gaiaExpor

private static void WriteGeometry(Geometry geom, GaiaExport gaiaExport, BinaryWriter bw)
{
WriteCoordinates writeCoordinates = SetWriteCoordinatesFunction(gaiaExport);
var writeCoordinates = SetWriteCoordinatesFunction(gaiaExport);

//Geometry type
int coordinateFlag = gaiaExport.CoordinateFlag;
Expand Down Expand Up @@ -173,7 +183,7 @@ private static void WriteGeometry(Geometry geom, GaiaExport gaiaExport, BinaryWr
private static void WriteGeometryCollection(GeometryCollection geom, GaiaExport gaiaExport, BinaryWriter bw)
{
gaiaExport.WriteInt32(bw, geom.NumGeometries);
for (var i = 0; i < geom.NumGeometries; i++)
for (int i = 0; i < geom.NumGeometries; i++)
{
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
WriteGeometry(geom[i], gaiaExport, bw);
Expand All @@ -183,7 +193,7 @@ private static void WriteGeometryCollection(GeometryCollection geom, GaiaExport
private static void WriteMultiPolygon(GeometryCollection geom, WriteCoordinates writeCoordinates, GaiaExport gaiaExport, BinaryWriter bw)
{
gaiaExport.WriteInt32(bw, geom.NumGeometries);
for (var i = 0; i < geom.NumGeometries; i++)
for (int i = 0; i < geom.NumGeometries; i++)
{
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
gaiaExport.WriteInt32(bw, gaiaExport.CoordinateFlag | (int)GaiaGeoGeometry.GAIA_POLYGON);
Expand All @@ -194,7 +204,7 @@ private static void WriteMultiPolygon(GeometryCollection geom, WriteCoordinates
private static void WriteMultiLineString(MultiLineString geom, WriteCoordinates writeCoordinates, GaiaExport gaiaExport, BinaryWriter bw)
{
gaiaExport.WriteInt32(bw, geom.NumGeometries);
for (var i = 0; i < geom.NumGeometries; i++)
for (int i = 0; i < geom.NumGeometries; i++)
{
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
gaiaExport.WriteInt32(bw, gaiaExport.CoordinateFlag | (int)GaiaGeoGeometry.GAIA_LINESTRING);
Expand All @@ -210,9 +220,9 @@ private static void WriteMultiPoint(MultiPoint geom, WriteCoordinates writeCoord
wi(bw, geom.NumGeometries);

// get the coordinate flag
var coordinateFlag = gaiaExport.CoordinateFlagUncompressed;
int coordinateFlag = gaiaExport.CoordinateFlagUncompressed;

for (var i = 0; i < geom.NumGeometries; i++)
for (int i = 0; i < geom.NumGeometries; i++)
{
//write entity begin marker
bw.Write((byte)GaiaGeoBlobMark.GAIA_MARK_ENTITY);
Expand All @@ -229,7 +239,7 @@ private static void WritePolygon(Polygon geom, WriteCoordinates writeCoordinates
{
gaiaExport.WriteInt32(bw, geom.NumInteriorRings + 1);
WriteLineString(geom.Shell, writeCoordinates, gaiaExport, bw);
for (var i = 0; i < geom.NumInteriorRings; i++)
for (int i = 0; i < geom.NumInteriorRings; i++)
WriteLineString(geom.GetInteriorRingN(i), writeCoordinates, gaiaExport, bw);
}

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

private static GaiaExport SetGaiaGeoExportFunctions(GaiaGeoEndianMarker gaiaGeoEndianMarker, bool hasZ, bool hasM, bool useCompression)
{
var conversionNeeded = false;
bool conversionNeeded = false;
switch (gaiaGeoEndianMarker)
{
case GaiaGeoEndianMarker.GAIA_LITTLE_ENDIAN:
Expand All @@ -274,7 +284,7 @@ private static void WriteXY(CoordinateSequence coordinateSequence, GaiaExport ex
{
var wd = export.WriteDouble;

for (var i = 0; i < coordinateSequence.Count; i++)
for (int i = 0; i < coordinateSequence.Count; i++)
{
var c = coordinateSequence.GetCoordinate(i);
wd(bw, c.X, c.Y);
Expand All @@ -284,7 +294,7 @@ private static void WriteXY(CoordinateSequence coordinateSequence, GaiaExport ex
private static void WriteXYZ(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
{
var wd = export.WriteDouble;
for (var i = 0; i < coordinateSequence.Count; i++)
for (int i = 0; i < coordinateSequence.Count; i++)
{
var c = coordinateSequence.GetCoordinate(i);
wd(bw, c.X, c.Y, c.Z);
Expand All @@ -294,7 +304,7 @@ private static void WriteXYZ(CoordinateSequence coordinateSequence, GaiaExport e
private static void WriteXYM(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
{
var wd = export.WriteDouble;
for (var i = 0; i < coordinateSequence.Count; i++)
for (int i = 0; i < coordinateSequence.Count; i++)
{
var c = coordinateSequence.GetCoordinate(i);
wd(bw, c.X, c.Y, coordinateSequence.GetOrdinate(i, Ordinate.M));
Expand All @@ -304,7 +314,7 @@ private static void WriteXYM(CoordinateSequence coordinateSequence, GaiaExport e
private static void WriteXYZM(CoordinateSequence coordinateSequence, GaiaExport export, BinaryWriter bw)
{
var wd = export.WriteDouble;
for (var i = 0; i < coordinateSequence.Count; i++)
for (int i = 0; i < coordinateSequence.Count; i++)
{
var c = coordinateSequence.GetCoordinate(i);
wd(bw, c.X, c.Y, c.Z, coordinateSequence.GetOrdinate(i, Ordinate.M));
Expand All @@ -320,14 +330,14 @@ private static void WriteCompressedXY(CoordinateSequence coordinateSequence, Gai
wd(bw, cprev.X, cprev.Y);

var ws = export.WriteSingle;
var maxIndex = coordinateSequence.Count - 1;
int maxIndex = coordinateSequence.Count - 1;
if (maxIndex <= 0) return;

for (var i = 1; i < maxIndex; i++)
for (int i = 1; i < maxIndex; i++)
{
var c = coordinateSequence.GetCoordinate(i);
var fx = (float)(c.X - cprev.X);
var fy = (float)(c.Y - cprev.Y);
float fx = (float)(c.X - cprev.X);
float fy = (float)(c.Y - cprev.Y);
ws(bw, fx, fy);
cprev = c;
}
Expand All @@ -345,16 +355,16 @@ private static void WriteCompressedXYZ(CoordinateSequence coordinateSequence, Ga
var cprev = coordinateSequence.GetCoordinate(0);
wd(bw, cprev.X, cprev.Y, cprev.Z);

var maxIndex = coordinateSequence.Count - 1;
int maxIndex = coordinateSequence.Count - 1;
if (maxIndex <= 0) return;

var ws = export.WriteSingle;
for (var i = 1; i < maxIndex; i++)
for (int i = 1; i < maxIndex; i++)
{
var c = coordinateSequence.GetCoordinate(i);
var fx = (float)(c.X - cprev.X);
var fy = (float)(c.Y - cprev.Y);
var fz = (float)(c.Z - cprev.Z);
float fx = (float)(c.X - cprev.X);
float fy = (float)(c.Y - cprev.Y);
float fz = (float)(c.Z - cprev.Z);
ws(bw, fx, fy, fz);
cprev = c;
}
Expand All @@ -368,19 +378,19 @@ private static void WriteCompressedXYM(CoordinateSequence coordinateSequence, Ga

// Write initial coordinate
var cprev = coordinateSequence.GetCoordinate(0);
var mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
double mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
wd(bw, cprev.X, cprev.Y, mprev);

var maxIndex = coordinateSequence.Count - 1;
int maxIndex = coordinateSequence.Count - 1;
if (maxIndex <= 0) return;

var ws = export.WriteSingle;
for (var i = 1; i < maxIndex; i++)
for (int i = 1; i < maxIndex; i++)
{
var c = coordinateSequence.GetCoordinate(i);
var fx = (float)(c.X - cprev.X);
var fy = (float)(c.Y - cprev.Y);
var fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
float fx = (float)(c.X - cprev.X);
float fy = (float)(c.Y - cprev.Y);
float fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
ws(bw, fx, fy, fm);
cprev = c;
}
Expand All @@ -395,20 +405,20 @@ private static void WriteCompressedXYZM(CoordinateSequence coordinateSequence, G

// Write initial coordinate
var cprev = coordinateSequence.GetCoordinate(0);
var mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
double mprev = coordinateSequence.GetOrdinate(0, Ordinate.M);
wd(bw, cprev.X, cprev.Y, cprev.Z, mprev);

var maxIndex = coordinateSequence.Count - 1;
int maxIndex = coordinateSequence.Count - 1;
if (maxIndex <= 0) return;

var ws = export.WriteSingle;
for (var i = 1; i < maxIndex; i++)
for (int i = 1; i < maxIndex; i++)
{
var c = coordinateSequence.GetCoordinate(i);
var fx = (float)(c.X - cprev.X);
var fy = (float)(c.Y - cprev.Y);
var fz = (float)(c.Z - cprev.Z);
var fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
float fx = (float)(c.X - cprev.X);
float fy = (float)(c.Y - cprev.Y);
float fz = (float)(c.Z - cprev.Z);
float fm = (float)(coordinateSequence.GetOrdinate(i, Ordinate.M) - mprev);
ws(bw, fx, fy, fz, fm);
cprev = c;
}
Expand Down
12 changes: 7 additions & 5 deletions test/NetTopologySuite.IO.SpatiaLite.Test/AbstractIOFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ protected set
throw new InvalidOperationException();

var oldPM = RandomGeometryHelper.Factory.PrecisionModel;
RandomGeometryHelper.Factory = RandomGeometryHelper.Factory is OgcCompliantGeometryFactory
? new OgcCompliantGeometryFactory(oldPM, value)
RandomGeometryHelper.Factory = RandomGeometryHelper.Factory is GeometryFactoryEx
? new GeometryFactoryEx(oldPM, value)
{ OrientationOfExteriorRing = LinearRingOrientation.CCW }
: new GeometryFactory(oldPM, value);
}
}
Expand All @@ -139,8 +140,9 @@ protected set
? factory.CoordinateSequenceFactory
: CoordinateArraySequenceFactory.Instance;

if (RandomGeometryHelper.Factory is OgcCompliantGeometryFactory)
RandomGeometryHelper.Factory = new OgcCompliantGeometryFactory(value, oldSrid, oldFactory);
if (RandomGeometryHelper.Factory is GeometryFactoryEx)
RandomGeometryHelper.Factory = new GeometryFactoryEx(value, oldSrid, oldFactory)
{OrientationOfExteriorRing = LinearRingOrientation.CCW };
else
RandomGeometryHelper.Factory = new GeometryFactory(value, oldSrid, oldFactory);
}
Expand Down Expand Up @@ -198,7 +200,7 @@ public Ordinates ClipOrdinates

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

Expand Down
25 changes: 11 additions & 14 deletions test/NetTopologySuite.IO.SpatiaLite.Test/GeoPackageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,25 @@ protected override void CreateTestStore()
if (File.Exists(Name))
File.Delete(Name);

using (var conn = new SQLiteConnection($"Data Source=\"{Name}\""))
{
conn.Open();
conn.EnableExtensions(true);
SpatialiteLoader.Load(conn);
using (var cmd = conn.CreateCommand())
{
cmd.CommandText =
"CREATE TABLE \"nts_io_geopackage\" (id int primary key, wkt text, the_geom blob);";
cmd.ExecuteNonQuery();
}
}
using var conn = new SQLiteConnection("Data Source=\"" + Name + "\"");
conn.Open();
conn.EnableExtensions(true);
SpatialiteLoader.Load(conn);

using var cmd = conn.CreateCommand();
cmd.CommandText =
"CREATE TABLE \"nts_io_geopackage\" (\"id\" INTEGER PRIMARY KEY, \"wkt\" TEXT, \"the_geom\" BLOB);";
cmd.ExecuteNonQuery();
}

protected override void CheckEquality(Geometry gIn, Geometry gParsed, WKTWriter writer)
{
var res = gIn.EqualsExact(gParsed);
bool res = gIn.EqualsExact(gParsed);
if (res) return;

if (Compressed)
{
var discreteHausdorffDistance =
double discreteHausdorffDistance =
Algorithm.Distance.DiscreteHausdorffDistance.Distance(gIn, gParsed);
if (discreteHausdorffDistance > 0.05)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.113.7" />
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
<PackageReference Include="mod_spatialite" Version="4.3.0.1" />
</ItemGroup>

Expand Down
6 changes: 3 additions & 3 deletions test/NetTopologySuite.IO.SpatiaLite.Test/SpatiaLiteFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ protected override void CreateTestStore()

using var cmd = conn.CreateCommand();
cmd.CommandText =
"CREATE TABLE \"nts_io_spatialite\" (id int primary key, the_geom geometry);";
"CREATE TABLE \"nts_io_spatialite\" (\"id\" INTEGER PRIMARY KEY, \"the_geom\" BLOB);";
cmd.ExecuteNonQuery();
}

protected override void CheckEquality(Geometry gIn, Geometry gParsed, WKTWriter writer)
{
var res = gIn.EqualsExact(gParsed);
bool res = gIn.EqualsExact(gParsed);
if (res) return;

if (Compressed)
{
var discreteHausdorffDistance =
double discreteHausdorffDistance =
Algorithm.Distance.DiscreteHausdorffDistance.Distance(gIn, gParsed);
if (discreteHausdorffDistance > 0.05)
{
Expand Down

0 comments on commit 9950042

Please sign in to comment.