gpt4 book ai didi

c# - 解析没有 shx 和 dbf 文件的 shapefile(shp 文件)

转载 作者:太空宇宙 更新时间:2023-11-03 15:26:20 25 4
gpt4 key购买 nike

我需要上传一个 shapefile 并将所有形状存储在数据库中。我花了两天时间找到了一个好的图书馆,但到目前为止我还没有找到任何完整的解决方案。

我尝试使用“Catfood.Shapefile”库解析该文件,但除了我没有的 .shp 文件之外,它还需要另外两个文件。

我设法用 Java 实现了一段代码,但现在我需要用 C# 重新实现它。

请给我一些建议!

最佳答案

好的,Shp文件是一个很好的设计文件,你可以看到描述here . shx 和 dbf 仅添加一些帮助来解析 shp 文件。我在 Catfood.Shapefile 中添加了一个类它的名字是 ShpParser。这个类的输入只是shp文件。它不需要 shx 和 dbf 文件来提取文件中的形状。

如果元数据为空,则两个类都会抛出异常,请确保从以下方法中删除元数据异常:

1- 形状构造器2- ShapeFactory.ParseShape

这里是ShpParser的代码

 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;
}
}

如何使用 ShpParser

using (var shapefile = new ShpParser("my.shp")
{
foreach (Shape shape in shapefile)
{
Console.WriteLine("ShapeType: {0}", shape.Type);
}
}

关于c# - 解析没有 shx 和 dbf 文件的 shapefile(shp 文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35180349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com