Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrTree implementation of RectangleSpatialIndex #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
104 changes: 104 additions & 0 deletions Tabula/RectangleSpatialIndex.Str.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using NetTopologySuite.Geometries;
using NetTopologySuite.Index.Strtree;
using System;
using System.Collections.Generic;
using System.Linq;
using UglyToad.PdfPig.Core;
using UglyToad.PdfPig.Geometry;

namespace Tabula
{
/// <summary>
/// ported from tabula-java/blob/master/src/main/java/technology/tabula/RectangleSpatialIndex.java
/// </summary>
/// <typeparam name="T"></typeparam>
public class RectangleSpatialIndex<T> where T : TableRectangle
{
private STRtree<T> si = new STRtree<T>();
private List<T> rectangles = new List<T>();

/// <summary>
///
/// </summary>
/// <param name="te"></param>
public void Add(T te)
{
rectangles.Add(te);
si.Insert(new Envelope(te.Left, te.Right, te.Bottom, te.Top), te);
}

/// <summary>
/// hack
/// </summary>
/// <param name="rectangle"></param>
private PdfRectangle Expand(PdfRectangle rectangle)
{
return new PdfRectangle(rectangle.Left - 1, rectangle.Bottom - 1, rectangle.Right + 1, rectangle.Top + 1);
}

/// <summary>
///
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public List<T> Contains(PdfRectangle r)
{
IList<T> intersection = si.Query(new Envelope(r.Left, r.Right, r.Top, r.Bottom));
List<T> rv = new List<T>();

foreach (T ir in intersection)
{
if (r.Contains(ir.BoundingBox))
{
rv.Add(ir);
}
}

Utils.Sort(rv, new TableRectangle.ILL_DEFINED_ORDER());
return rv;
}

/// <summary>
///
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public List<T> Contains(TableRectangle r)
{
IList<T> intersection = si.Query(new Envelope(r.Left, r.Right, r.Top, r.Bottom));
List<T> rv = new List<T>();

foreach (T ir in intersection)
{
if (r.Contains(ir))
{
rv.Add(ir);
}
}

Utils.Sort(rv, new TableRectangle.ILL_DEFINED_ORDER());
return rv;

}

/// <summary>
///
/// </summary>
/// <param name="r"></param>
/// <returns></returns>
public List<T> Intersects(TableRectangle r)
{
IList<T> rv = si.Query(new Envelope(r.Left, r.Right, r.Top, r.Bottom));
return (List<T>)rv;

}

/// <summary>
/// Minimum bounding box of all the Rectangles contained on this RectangleSpatialIndex.
/// </summary>
public TableRectangle GetBounds()
{
return TableRectangle.BoundingBoxOf(rectangles.Cast<TableRectangle>());
}
}
}
106 changes: 81 additions & 25 deletions Tabula/Tabula.csproj
Original file line number Diff line number Diff line change
@@ -1,29 +1,85 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0;net452;net46;net461;net462;net47;net5.0;net6.0</TargetFrameworks>
<Description>Extract tables from PDF files (port of tabula-java using PdfPig).</Description>
<PackageProjectUrl>https://github.com/BobLd/tabula-sharp</PackageProjectUrl>
<Version>0.1.2</Version>
<Authors>BobLd</Authors>
<Company>BobLd</Company>
<PackageTags>pdf, extract, table, tabula, pdfpig, parse, extraction, export</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/BobLd/tabula-sharp</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="PdfPig" Version="0.1.5" />
</ItemGroup>
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0;net452;net46;net461;net462;net47;net5.0;net6.0</TargetFrameworks>
<Description>Extract tables from PDF files (port of tabula-java using PdfPig).</Description>
<PackageProjectUrl>https://github.com/BobLd/tabula-sharp</PackageProjectUrl>
<Version>0.1.2</Version>
<Authors>BobLd</Authors>
<Company>BobLd</Company>
<PackageTags>pdf, extract, table, tabula, pdfpig, parse, extraction, export</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/BobLd/tabula-sharp</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<Compile Remove="RectangleSpatialIndex.cs" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="PdfPig" Version="0.1.5" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
<Compile Include="RectangleSpatialIndex.cs" />
<Compile Remove="RectangleSpatialIndex.Str.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net46'">
<Compile Include="RectangleSpatialIndex.cs" />
<Compile Remove="RectangleSpatialIndex.Str.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net47'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="NetTopologySuite">
<Version>2.4.0</Version>
</PackageReference>
</ItemGroup>

</Project>