C# 不使用shx和dbf文件解析shapefile(shp文件)
我需要上传一个shapefile并将所有形状存储在数据库中。我花了两天时间寻找一个好的图书馆,但到目前为止,我还没有找到任何完整的解决方案 我试图用“Catfood.Shapefile”库解析该文件,但除了.shp文件之外,它还需要另外两个我没有的文件 我设法用Java实现了一段代码,但现在我需要用c重新实现它C# 不使用shx和dbf文件解析shapefile(shp文件),c#,shapefile,C#,Shapefile,我需要上传一个shapefile并将所有形状存储在数据库中。我花了两天时间寻找一个好的图书馆,但到目前为止,我还没有找到任何完整的解决方案 我试图用“Catfood.Shapefile”库解析该文件,但除了.shp文件之外,它还需要另外两个我没有的文件 我设法用Java实现了一段代码,但现在我需要用c重新实现它 请给我一些建议 好的,Shp文件是一个井设计文件,您可以看到描述。shx和dbf只为解析shp文件添加了一些帮助。我添加了一个名为ShpParser的类。此类的输入仅为Shp文件。它不需
请给我一些建议 好的,Shp文件是一个井设计文件,您可以看到描述。shx和dbf只为解析shp文件添加了一些帮助。我添加了一个名为ShpParser的类。此类的输入仅为Shp文件。它不需要shx和dbf文件来提取文件中的形状 如果元数据为null,则会引发两个类异常,请确保从以下方法中删除元数据异常: 1-形构造函数 2-ShapeFactory.ParseShape 这里是语法分析器的代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.OleDb;
using System.IO;
using System.Text;
namespace Catfood.Shapefile
{
/// <summary>
///
/// </summary>
public class ShpParser : IDisposable, IEnumerator<Shape>, IEnumerable<Shape>
{
private bool _disposed;
private bool _opened;
private int _currentIndex;
private int _count;
private RectangleD _boundingBox;
private ShapeType _type;
private int _currentPosition;
private FileStream _mainStream;
private Header _mainHeader;
/// <summary>
///
/// </summary>
public ShpParser()
{
_currentIndex = -1;
_currentPosition = 0;
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
public ShpParser(string path)
: this()
{
Open(path);
}
/// <summary>
///
/// </summary>
/// <param name="path"></param>
private void Open(string path)
{
if (_disposed)
{
throw new ObjectDisposedException("ShpParser");
}
if (string.IsNullOrEmpty(path))
{
throw new ArgumentNullException("path");
}
if (!File.Exists(path))
{
throw new FileNotFoundException("shp file not found", path);
}
_mainStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
if (_mainStream.Length < Header.HeaderLength)
{
throw new InvalidOperationException("Shapefile main file does not contain a valid header");
}
// read in and parse the headers
var headerBytes = new byte[Header.HeaderLength];
_mainStream.Read(headerBytes, 0, Header.HeaderLength);
_mainHeader = new Header(headerBytes);
_type = _mainHeader.ShapeType;
_boundingBox = new RectangleD(_mainHeader.XMin, _mainHeader.YMin, _mainHeader.XMax, _mainHeader.YMax);
_currentPosition = Header.HeaderLength;
_count = _mainHeader.FileLength * 2; // FileLength return the size of file in words (16 bytes)
_opened = true;
}
#region IDisposable Members
/// <summary>
/// Close the Shapefile. Equivalent to calling Dispose().
/// </summary>
public void Close()
{
Dispose();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool canDisposeManagedResources)
{
if (!_disposed)
{
if (canDisposeManagedResources)
{
if (_mainStream != null)
{
_mainStream.Close();
_mainStream = null;
}
}
_disposed = true;
_opened = false;
}
}
#endregion
public bool MoveNext()
{
if (_disposed) throw new ObjectDisposedException("ShpParser");
if (!_opened) throw new InvalidOperationException("ShpParser not open.");
if (_currentPosition < (_count - 1))
{
// try to read the next database record
return true;
}
else
{
// reached the last shape
return false;
}
}
public void Reset()
{
if (_disposed) throw new ObjectDisposedException("ShpParser");
if (!_opened) throw new InvalidOperationException("ShpParser not open.");
_currentIndex = -1;
}
public Shape Current
{
get
{
if (_disposed) throw new ObjectDisposedException("ShpParser");
if (!_opened) throw new InvalidOperationException("ShpParser not open.");
//get recordheader;
var recordheader = new byte[8];
_mainStream.Read(recordheader, 0, recordheader.Length);
int contentOffsetInWords = EndianBitConverter.ToInt32(recordheader, 0, ProvidedOrder.Big);
int contentLengthInWords = EndianBitConverter.ToInt32(recordheader, 4, ProvidedOrder.Big);
_mainStream.Seek(_currentPosition , SeekOrigin.Begin); // back to header of record
int bytesToRead = (contentLengthInWords * 2) + 8;
byte[] shapeData = new byte[bytesToRead];
_currentPosition += bytesToRead;
_mainStream.Read(shapeData, 0, bytesToRead);
return ShapeFactory.ParseShape(shapeData , null);
// return null;
}
}
object IEnumerator.Current
{
get
{
if (_disposed) throw new ObjectDisposedException("ShpParser");
if (!_opened) throw new InvalidOperationException("ShpParser not open.");
return this.Current;
}
}
public IEnumerator<Shape> GetEnumerator()
{
return (IEnumerator<Shape>)this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator)this;
}
}
using (var shapefile = new ShpParser("my.shp")
{
foreach (Shape shape in shapefile)
{
Console.WriteLine("ShapeType: {0}", shape.Type);
}
}
这些文件是必需的。shapefile不是独立的文件。它实际上是形状、索引和元数据文件的组合。没有它们,您只有一堆没有意义元数据的点。找到丢失的shapefile文件并将其添加到同一文件夹,或者检查是否可以强制库在没有它们的情况下工作。但是,如果没有元数据,您将如何将形状与任何其他数据匹配并使用它们呢;int avl=fis.available();prefs.setMaxNumberOfPointsPerShape(16650);shapeFileReader=新的shapeFileReader(fis,prefs);我有这两个文件那问题是什么?为什么不将这些文件与C#代码一起使用呢?这些可用的java库不是C#:)shx和dbf文件不是库。它们是与shp类似的数据文件。事实上,这三个文件不应该分开。只需将它们与
shp
文件放在同一文件夹中即可。这些文件是必需的。没有它们,就没有形状文件