Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: .NET Core

on:
push:
branches: [ develop ]

jobs:
develop:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
submodules: 'true'

- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x

- name: Add GitHub Nuget Source
run: dotnet nuget add source https://nuget.pkg.github.com/osmsharp/index.json -n osmsharp -u xivk -p ${{secrets.PACKAGES_SECRET }} --store-password-in-clear-text

- name: Restore packages.
run: dotnet restore
- name: Build all projects.
run: dotnet build --configuration Release --no-restore
- name: Unittests.
run: dotnet test
working-directory: ./test/OsmSharp.IO.Binary.Test/
- name: Functional tests.
run: dotnet run -c release
working-directory: ./test/OsmSharp.IO.Binary.Test.Functional/
- name: Nuget Pack
run: dotnet pack -c release
working-directory: ./src/OsmSharp.IO.Binary/
- name: Nuget push
run: dotnet nuget push **/*.nupkg --skip-duplicate -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/osmsharp/index.json
working-directory: ./src/
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ TestResults*/
*.vspx
*.patch
*.osm.pbf
*.osm.bin
TestResult.xml
.vs*/
.idea*/
Expand All @@ -50,9 +49,13 @@ src/OsmSharp.IO.Binary.CLI/**/*.osm
src/OsmSharp.IO.Binary.CLI/**/*.db
src/OsmSharp.IO.Binary.CLI/**/*.hash
src/OsmSharp.IO.Binary.CLI/**/*.osc
test/OsmSharp.IO.Binary.Test.Functional/**/*.osm
test/OsmSharp.IO.Binary.Test.Functional/**/*.osm.bin
test/OsmSharp.IO.Binary.Test.Functional/**/*.osm.pbf

.idea*/


**/logs/*
**/*.bin.zip
**/*.bin.zip
test*.osm
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// The MIT License (MIT)

// Copyright (c) 2017 Ben Abelshausen
// Copyright (c) 2021 Ben Abelshausen

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ An IO module on top of OsmSharp that reads/writes OSM data in a **custom binary

We built this because it more efficient compared to OSM-XML and has some advantages over OSM-PBF. This can be used to:

- Read/write indivdual objects.
- Read/write individual objects.
- Read/write objects with negative ids
- Read/write objects missing data (even missing ids, versions, etc.).
- Can be streamed.
Expand Down
2 changes: 1 addition & 1 deletion src/OsmSharp.IO.Binary.CLI/OsmSharp.IO.Binary.CLI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\OsmSharp.IO.Binary\OsmSharp.IO.Binary.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion src/OsmSharp.IO.Binary/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static class BinarySerializer
/// <summary>
/// Appends the header byte(s).
/// </summary>
private static void AppendHeader(this Stream stream, OsmGeo osmGeo)
public static void AppendHeader(this Stream stream, OsmGeo osmGeo)
{
// build header containing type and nullable flags.
byte header = 1; // a node.
Expand Down
4 changes: 2 additions & 2 deletions src/OsmSharp.IO.Binary/OsmSharp.IO.Binary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>OsmSharp.IO.Binary</AssemblyName>
<PackageId>OsmSharp.IO.Binary</PackageId>
<PackageVersion>0.2.8-alpha</PackageVersion>
<PackageVersion>0.3.1-pre001</PackageVersion>
<Title>OsmSharp.IO.Binary</Title>
<Authors>Ben Abelshausen</Authors>
<Description>A binary IO package for OsmSharp.</Description>
Expand All @@ -15,6 +15,6 @@
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="OsmSharp" Version="7.0.0-pre014" />
<PackageReference Include="OsmSharp" Version="7.0.0-pre018" />
</ItemGroup>
</Project>
143 changes: 143 additions & 0 deletions src/OsmSharp.IO.Binary/v0_1/BinaryOsmStreamSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System;
using System.IO;
using OsmSharp.Streams;

namespace OsmSharp.IO.Binary.v0_1
{

/// <summary>
/// A stream source that just reads objects in binary format.
/// </summary>
public class BinaryOsmStreamSource : OsmStreamSource
{
private readonly Stream _stream;
private readonly byte[] _buffer;
private readonly long? _initialPosition;

/// <summary>
/// Creates a new binary stream source.
/// </summary>
public BinaryOsmStreamSource(Stream stream)
{
_stream = stream;
if (_stream.CanSeek)
{
_initialPosition = _stream.Position;
}
_buffer = new byte[1024];
}

/// <summary>
/// Returns true if this source can be reset.
/// </summary>
public override bool CanReset => _stream.CanSeek;

/// <summary>
/// Returns the current object.
/// </summary>
/// <returns></returns>
public override OsmGeo Current()
{
return _current;
}

private OsmGeo _current;
private long? _firstWayPosition;
private long? _firstRelationPosition;

/// <summary>
/// Move to the next object in this stream source.
/// </summary>
public override bool MoveNext(bool ignoreNodes, bool ignoreWays, bool ignoreRelations)
{
if (_stream.CanSeek)
{
if (_stream.Length == _stream.Position + 1)
{
return false;
}

// move to first way/relation position if they are known and nodes and or ways are to be skipped.
if (_firstWayPosition != null && ignoreNodes && !ignoreWays)
{
// if nodes have to be ignored, there was already a first pass and ways are not to be ignored jump to the first way.
if (_stream.Position <= _firstWayPosition)
{
// only just to the first way if that hasn't happened yet.
_stream.Seek(_firstWayPosition.Value, SeekOrigin.Begin);
}
}

if (_firstRelationPosition != null && ignoreNodes && ignoreWays && !ignoreRelations)
{
// if nodes and ways have to be ignored, there was already a first pass and ways are not be ignored jump to the first relation.
if (_stream.Position < _firstRelationPosition)
{
// only just to the first relation if that hasn't happened yet.
_stream.Seek(_firstRelationPosition.Value, SeekOrigin.Begin);
}
}
}

long? positionBefore = null;
if (_stream.CanSeek) positionBefore = _stream.Position;
var osmGeo = this.DoMoveNext();
while (osmGeo != null)
{
switch (osmGeo.Type)
{
case OsmGeoType.Node:
if (!ignoreNodes)
{
_current = osmGeo;
return true;
}

break;
case OsmGeoType.Way:
if (_firstWayPosition == null && positionBefore != null) _firstWayPosition = positionBefore;
if (!ignoreWays)
{
_current = osmGeo;
return true;
}

break;
case OsmGeoType.Relation:
if (_firstRelationPosition == null && positionBefore != null)
_firstRelationPosition = positionBefore;
if (!ignoreRelations)
{
_current = osmGeo;
return true;
}

break;
default:
throw new ArgumentOutOfRangeException();
}

osmGeo = this.DoMoveNext();
}

return false;
}

private OsmGeo DoMoveNext()
{
return BinarySerializer.ReadOsmGeo(_stream, _buffer);
}

/// <summary>
/// Resets this stream.
/// </summary>
public override void Reset()
{
if (_initialPosition == null) throw new NotSupportedException(
$"Cannot reset this stream, source stream is not seekable, check {nameof(this.CanReset)} before calling {nameof(this.Reset)}");

_current = null;
_stream.Seek(_initialPosition.Value, SeekOrigin.Begin);
}
}
}
55 changes: 55 additions & 0 deletions src/OsmSharp.IO.Binary/v0_1/BinaryOsmStreamTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.IO;

namespace OsmSharp.IO.Binary.v0_1
{
/// <summary>
/// A stream target that just writes objects in binary format.
/// </summary>
public class BinaryOsmStreamTarget : OsmSharp.Streams.OsmStreamTarget
{
private readonly Stream _stream;

/// <summary>
/// Creates a new stream target.
/// </summary>
public BinaryOsmStreamTarget(Stream stream)
{
_stream = stream;
}

/// <summary>
/// Adds a node.
/// </summary>
/// <param name="node"></param>
public override void AddNode(Node node)
{
_stream.Append(node);
}

/// <summary>
/// Adds a relation.
/// </summary>
/// <param name="relation"></param>
public override void AddRelation(Relation relation)
{
_stream.Append(relation);
}

/// <summary>
/// Adds a way.
/// </summary>
/// <param name="way"></param>
public override void AddWay(Way way)
{
_stream.Append(way);
}

/// <summary>
/// Initializes this target.
/// </summary>
public override void Initialize()
{

}
}
}
Loading
Loading