Shape files数据说明
shape文件的操作

shape⽂件的操作⼀、maven依赖引⼊<dependency><groupId>org.geotools</groupId><artifactId>gt-api</artifactId><exclusions><exclusion><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId></exclusion><exclusion><groupId>jgridshift</groupId><artifactId>jgridshift</artifactId></exclusion><exclusion><groupId>Javax.media</groupId><artifactId>jai_core</artifactId></exclusion></exclusions></dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId></dependency><dependency><groupId>jgridshift</groupId><artifactId>jgridshift</artifactId></dependency><dependency><groupId>Javax.media</groupId><artifactId>jai_core</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-geojson</artifactId></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-epsg-hsql</artifactId><exclusions><exclusion><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId></exclusion></exclusions></dependency><!-- https:///artifact/org.hsqldb/hsqldb --><dependency><groupId>org.hsqldb</groupId><artifactId>hsqldb</artifactId><scope>test</scope></dependency><dependency><groupId>org.geotools</groupId><artifactId>gt-shapefile</artifactId></dependency><dependency><groupId>com.googlecode.json-simple</groupId><artifactId>json-simple</artifactId></dependency>备注:需要配置下载org.geotools的私有仓库<repositories><repository><id>osgeo</id><name>OSGeo Release Repository</name><url>https:///repository/release/</url><snapshots><enabled>false</enabled></snapshots><releases><enabled>true</enabled></releases></repository></repositories>⼆、读取shape⽂件,并转换为对象Tpublic static List<T>readShape(URL url) throws IOException {ShapefileDataStore store = new ShapefileDataStore(url);//设置编码Charset charset = Charset.forName("GBK");store.setCharset(charset);SimpleFeatureSource sfSource = store.getFeatureSource();SimpleFeatureIterator sfIter = sfSource.getFeatures().features();// 从ShapeFile⽂件中遍历每⼀个Feature,然后将Feature转为GeoJSON字符串List<T> list = new ArrayList<>();while (sfIter.hasNext()) {SimpleFeature feature = sfIter.next();// Feature转GeoJSONFeatureJSON fjson = new FeatureJSON();StringWriter writer = new StringWriter();fjson.writeFeature(feature, writer);JSONObject jsonObject = new JSONObject(writer.toString());String type = jsonObject.getStr("type").toLowerCase();if (type.equals("multipolygon")){LOGGER.error("该数据不是图斑:【{}】",writer);continue;}String properties = jsonObject.remove("properties").toString();jsonObject.remove("id");//其他属性T t = JSONUtil.toBean(properties, T.class);if (JudgeUtil.isDBNull(t.getObjectid())){LOGGER.error("该图斑每页objectid:【{}】",writer.toString());continue;}//geojsont.setGeojson(jsonObject.toString());list.add(gisWarnQrstbhhx);}return list;}三、shape⽂件写⼊1、写⼊shape⽂件⼊⼝/*** 写shape⽂件* @param rootFile* @param shapeName shape⽂件名包含后缀名.shp* @param list*/public static void writeShape(File rootFile,String shapeName,List<T>list) throws IOException {if (JudgeUtil.isEmpty(list)){throw new WrongDataException("数据不能为空");}String type = new JSONObject(list.get(0).getGeojson()).getStr("type").toLowerCase();//将list转换为geoJson字符串String geoJson = getGsoJson(list);if (!rootFile.exists() || rootFile.isFile()){rootFile.mkdirs();}File file = new File(rootFile,shapeName);Map<String, Serializable> params = new HashMap<>();params.put(ShapefileDataStoreFactory.URLP.key,file.toURI().toURL());ShapefileDataStore ds = (ShapefileDataStore) new ShapefileDataStoreFactory().createNewDataStore(params);//定义图形信息和属性信息SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();tb.setCRS(DefaultGeographicCRS.WGS84);tb.setName("shapefile");tb.setDefaultGeometry("the_geom");//添加属性addProperties(type,tb);ds.createSchema(tb.buildFeatureType());//设置编码Charset charset = Charset.forName("GBK");ds.setCharset(charset);FeatureJSON featureJson = new FeatureJSON();SimpleFeatureCollection featureCollection = (SimpleFeatureCollection) featureJson.readFeatureCollection(geoJson);FeatureIterator<SimpleFeature> iterator = featureCollection.features();//设置WriterFeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds.getFeatureWriter(ds.getTypeNames()[0], Transaction.AUTO_COMMIT);//写下⼀条SimpleFeature srcFeature;Iterator<Property> properties;while (iterator.hasNext()){srcFeature = iterator.next();properties = srcFeature.getProperties().iterator();SimpleFeature destFeature = writer.next();while (properties.hasNext()){Name name = properties.next().getName();Object attribute = srcFeature.getAttribute(name);if (!(attribute instanceof Geometry)){destFeature.setAttribute(name, attribute);}}destFeature.setDefaultGeometryProperty(srcFeature.getDefaultGeometryProperty());}writer.write();writer.close();ds.dispose();}2、将List<T>转换为geojson/*** T转geojson* @param list* @return*/private static String getGsoJson(List<T>list){JSONObject jsonObject = new JSONObject();jsonObject.putOnce("type","FeatureCollection");JSONArray jsonArray = new JSONArray();jsonObject.putOnce("features",jsonArray);for (T t : list) {JSONObject feature = new JSONObject();feature.putOnce("type","Feature");feature.putOnce("geometry",new JSONObject(t.getGeojson()));t.setGeojson(null);t.setGeom(null);t.setGid(null);feature.putOnce("properties",JSONUtil.parseObj(t));jsonArray.add(feature);}return jsonObject.toString();}3、通过反射添加properties/*** 通过反射添加properties* @param tb*/private static void addProperties(String type, SimpleFeatureTypeBuilder tb){Field[] declaredFields = T.class.getDeclaredFields();setType(tb,type.toLowerCase());for (Field declaredField : declaredFields) {String name = declaredField.getName();if (!name.equals("serialVersionUID") && !name.equals("geojson") && !name.equals("geom")){ tb.add(name,declaredField.getType());}}}4、设置类型private static void setType(SimpleFeatureTypeBuilder tb,String type){if (type.equals("point")) {tb.add("the_geom", Point.class);} else if (type.equals("line")) {tb.add("the_geom", LineString.class);} else if (type.equals("polygon")) {tb.add("the_geom", Polygon.class);} else if (type.equals("multipoint")) {tb.add("the_geom", MultiPoint.class);} else if (type.equals("multiline")) {tb.add("the_geom", MultiLineString.class);} else if (type.equals("multipolygon")) {tb.add("the_geom", MultiPolygon.class);}}。
Shapefile文件类型

Shapefile文件类型
shapefile文件属于矢量数据中无拓扑关系的数据类型。
它至少包含3个文件,dbf、shp、shx 。
其中:dbf文件时存储矢量数据的属性信息的;
shp是存储矢量数据的空间信息的。
shx是保留要素几何学特征的索引文件。
另外,shapefile文件还可以包括其它文件,如sbn 、sbx 、prj等。
sbn 、sbx 也是空间索引文件,.prj属于数据的投影文件。
当中.shp、.shx、.dbf三个是万万不能删除的,删除掉一个就出错了。
shp没了一切全都没了,正所谓的皮之不存,毛将焉附。
删掉shx虽然打不开文件,但有工具能修复。
删掉.dbf文件也能修复,可属性是找不回来了。
删掉.prj文件会导致投影信息丢失。
.sbn 、.sbx 文件删掉后将丢失所建的索引。
使用PYTHON SHAPEFILE LIBRARY创建和编辑SHAPEFILE文件

使用Python Shapefile Library创建和编辑Shapefile文件shapefile是GIS中非常重要的一种数据类型,在ArcGIS中被称为要素类(Feature Classes),主要包括点(point)、线(polyline)和多边形(polygon)。
Python脚本是ArcGIS官方推荐的脚本语言,通过Python脚本能够很方便的调用ArcGIS中的各种工具和函数批量完成所需操作。
本文所介绍的这个库(Python Shapefile Library)是一个Python库,用于在Python脚本中对ArcGIS中的Shapefile 文件(.shp,.shx,.dbf等格式)进行读写操作。
1、Python Shapefile Library的下载与安装:Python Shapefile Library下载地址:https:///p/pyshp/Python Shapefile Library使用时无需安装,只需在Python程序中导入该模块文件即可(import shapefile,具体导入方法参考Python教程中模块的导入部分)2、Shapefile文件的读操作2.1 Python Shapefile Library提供了Reader类,通过创建Reader类的对象(如下面的sf)进行shapefile文件的读操作:sf = shapefile . Reader ('shapefile name')2.2 使用Python Shapefile Library读取shapefile文件的"几何数据"(Geometry)和"属性数据"(Attribute Record)"几何数据"一般有多个几何对象组成,比如一个"点文件",每个点就是一个对象;对于一个多边形文件,每个对象可能包含有多个多边形,每个多边形又称为"块(parts)",每个"块"由多个点组成。
ArcGISshape文件要素类包括点,面,折线,多点,多面数据表字段以及数据的查看,添加。。。

ArcGISshape⽂件要素类包括点,⾯,折线,多点,多⾯数据表字段以及数据的查看,添加。
⾸先shape⽂件是怎么建⽴的呢?======================打开ArcGIS Desktop中的Catalog,在⽬录树列表中,选择⼀个⽂件夹(如果有ArcSDE GeoDatabase则省去这步),在⽂件夹点击右键,新建ShapeFile⽂件,或者在空间数据库中New -》要素类FeatureClass(两者其实⼀样⼀样的),输⼊名称,和选择要素类型(有五种点线⾯多点多⾯||),坐标系也选择⼀个,⼀般要和其他要素⼀样,⽐如都是China的那个某年的坐标系,点击确定就创建了shape⽂件(在地理空间数据库中就是建⽴了⼀个表,⼀个要素类⽽已,下⾯全部按照shape单独的⽂件来说,地理空间数据库其实⼀样。
)。
Shape⽂件添加删除字段,暂时⽆法修改字段============================在shape⽂件上点击右键,打开【属性】,选择字段,默认有FID、Shape和id三个字段,FID就是⾃动增长的⼀个id,Shape是类型,这俩必须有,还必须有第三个字段,ArcGIS10好像不能修改id字段,但是可以先添加⼀个字段,然后再id这⼀⾏最左边,点击选择整⾏进⾏删除。
这样就可以查看,添加或删除字段了,⾄于修改,和更改顺序(其实更改顺序只是给⼈看的,没啥⽤),我暂时不知道如何操作。
Shape⽂件中对表进⾏数据的添加删除修改操作============================在有了字段后,表还是空的,这⾥的表其实和数据库⾥的表table的概念是⼀模⼀样的。
有了表结构,下⾯就是对表内容进⾏进⾏添加删除修改了。
这个操作需要使⽤ArcGIS Desktop⾥的 ArcMap软件。
打开ArcMap之后,⾸先把编辑器和绘图⼯具条调⽤出来。
在【⾃定义】【⾃定义模式】中可以操作,简单的也可以在⼯具栏空⽩处点击右键直接打开这些⼯具条。
shapefile包的用法

shapefile包的用法
shapefile是一种常用的地理信息系统(GIS)文件格式,用于存储地理空间数据。
它通常由多个文件组成,包括.shp、.shx、.dbf 和其他附加文件。
shapefile包是一个用于Python的开源库,用于读取和处理shapefile文件。
下面是shapefile包的常见用法:
1. 安装shapefile包:使用pip命令安装shapefile包,可以在终端或命令提示符中运行以下命令。
2. 导入shapefile库。
3. 读取shapefile文件:使用shapefile.Reader()函数来读取shapefile文件。
4. 访问shapefile属性:可以通过sf对象的属性来获取shapefile的一些基本信息。
5. 访问图形几何数据:可以使用sf对象的shapes()方法来访问shapefile中的图形几何数据。
每个图形都表示一个地理要素,例如点、线或多边形。
6. 访问属性数据:shapefile文件通常包含与每个地理要素相关联的属性数据。
可以使用sf对象的records()方法来获取属性数据。
7. 迭代访问shapefile中的要素:可以使用sf对象的iterShapes()和iterRecords()方法来迭代访问shapefile中的所有要素和属性值。
这些是shapefile包的基本用法。
通过了解shapefile包的功能和方法,你可以更好地处理和分析存储在shapefile文件中的地理空间数据。
shape文件的结构和组织

一、shape文件的结构和组织:1、shape文件技术描述:一个ESRI shape文件由主文件,索引文件,和一个dBASE表组成,主文件是一个可以直接访问、长度可变的文件,在主文件中每条记录用一系列的点描述一个物体,在索引文件中,每条记录保存主文件中相应的物体相对主文件头的偏移量。
在dBASE表中包含每条记录的属性,这种几何形与属性一对一的关系是依据纪录的数量的,dBASE文件中的属性纪录和主文件中的纪录必须是顺序是一样的。
2、数据类型:整型(Integer)32-bit(4 bytes)双精度(double)64-bit (8 bytes)二、主文件的组织主文件包含一个固定长度的文件头和一系列可变长的记录集,每一个可变长的纪录由一个固定长度的纪录头和一个可变长的纪录内容。
如下图:图一主文件的组织二进制正常状态:所有shape文件中的内容可以分成两种类型:(1)数据相关的:·主文件记录内容·主文件头数据表述区域(物体类型等)(2)文件管理相关的:·文件和记录的长度·纪录的偏移量等等主文件头:主文件头长度是100 个字节,下表展示了头文件各个成员包括(字节位置,值,类型,字节状态),在这个表中,位置是从文件头算起的。
File Length上表中File Length 是整个文件的长度,包括该头文件的100个字节。
所有的no_NULL shape type必须是同种类型。
下面是shape type的值:shape type上述shape type中2 、4、6等和33没有列入清单,这些保留数字是为了将来再用。
现在每个shape文件只能指定上述的类型中的一种。
Bounding Box纪录所有物体的x,y,z范围。
如果是“no data”类型,才用到Mmin和Mmax纪录头记录头包含纪录数量(该纪录是第几个纪录,第一个的值是1)和记录内容的长度。
记录头长度是8个字节,如下表,其中position是相对该纪录的开始位置。
ESRI Shape文件 技术说明书

ESRI Shape文件技术说明书ESRI白皮书—1998年7月初译:yzg2006年5月4日注意:本人非英语、计算机及GIS专业从业人员,翻译此文仅供参考之用。
受本人水平所限,出现错误之处实属正常,如有有心人能给予指正,将不胜感激。
2006年5月4日YZGESRI Shapefile 技术说明书ESRI 白皮书Contents pageWhy Shapefiles? 1Shapefile Technical Description 2Organization of the Main File 3Main File Record Contents 5Organization of the Index File 5Organization of the dBASE File 18Glossary 20ESRI Shapefile 技术说明书This document defines the shapefile(.shp)spatial data format and describes why shapefiles are important. It lists the tools available in Environmental Systems Research Institute, Inc.(ESRI),software for creating shapefiles directly or converting data into shapefiles from other formats. This document also provides all the technical information necessary for writing a computer program to create shapefiles without the use of ESRI® software for organizations that want to write their own fata translators.Why Shapefiles?A shapefile stores nontopological geometry and attribute information for the spatial features in a data set. The geometry for a feature is stored as a shape comprising a set of vector coordinates.Because shapefiles do not have the processing overhead of a topological data structure, they have advantages over other data sources, such as faster drawing speed and edit ability. Shapefiles handle single features that overlap or that are noncontiguous. They also typically require less disk space and are easier to read and write.Shapefiles can support point, line, and area features. Area features are represented as closed loop, double-digitized polygons. Attributes are held in a dBASE® format file. Each attribute record has a one-to-one relationship with the associated shape record.如何创建Shape文件Shape文件可以通过以下四种方法建立:■导出-可以通过ARC/INFO、PC ARC/INFO、Spatial Database Engine TM(SDE TM)、ArcView® GIS 或者Business MAP TM software等软件将任何源数据导出为Shape文件。
Shapefile

一、什么是shapefile?Shapefile是一种用于存储地理要素的几何位置和属性信息的非拓扑简单格式。
shapefile 中的地理要素可通过点、线或面(区域)来表示。
包含shapefile的工作空间还可以包含dBASE 表,它们用于存储可连接到shapefile的要素的附加属性。
(一)下面是shapefile如何在ArcCatalog中显示的示例。
还能看到dBASE文件(它可能与shapefile相关联)。
默认情况下,具有文件扩展名.txt、.asc、.csv或.tab 的所有文件将以文本文件的形式显示在ArcCatalog中。
但在选项对话框中,您可以选择其中哪些文件类型应显示为文本文件以及哪些不能显示在目录树中。
当文本文件包含逗号分隔和制表符分隔的值时,您能够在ArcCatalog表视图中看到它们的内容并可将其连接到地理要素。
在ArcCatalog中,可将文本文件删除,但其内容为只读。
可以在图层“属性”对话框的连接和关联选项卡中,将dBASE表或文本文件中存储的属性连接到shapefile中的要素。
如果表包含描述空间位置的信息(例如,x,y,z坐标或街道地址),则可以使用ArcCatalog中提供的工具创建用以表示这些位置的shapefile。
(二)编辑shapefile可以在具有任意许可等级的ArcGIS for Desktop(ArcGIS for Desktop Basic、ArcGIS for Desktop Standard或ArcGIS for Desktop Advanced)中编辑shapefile。
但要想利用高级编辑功能(例如,拓扑),则需要将shapefile作为要素类导入到地理数据库中。
(三)将shapefile和dBASE表导入到地理数据库要素类和表shapefile中的所有要素类型都会在地理数据库中转换为几何类型。
与coverage 不同的是,shapefile要素类型与地理数据库中存储的几何类型相类似,因此转换要更为简单。
Shapefile文件详解

Shapefile文件是美国环境系统研究所(ESRI)所研制的GIS文件系统格式文件,是工业标准的矢量数据文件。
Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何对象存为以坐标点集表示的图形文件—SHP文件,Shapefile文件并不含拓扑(Topological)数据结构。
一个Shape文件包括三个文件:一个主文件(*.shp),一个索引文件(*.shx),和一个dBASE(*.dbf)表。
主文件是一个直接存取,变长度记录的文件,其中每个记录描述构成一个地理特征(Feature)的所有vertices坐标值。
在索引文件中,每条记录包含对应主文件记录距离主文件头开始的偏移量,dBASE表包含SHP文件中每一个Feature的特征属性,表中几何记录和属性数据之间的一一对应关系是基于记录数目的ID。
在dBASE文件中的属性记录必须和主文件中的记录顺序是相同的。
图形数据和属性数据通过索引号建立一一对应的关系。
Shapefile中坐标文件(.shp)由固定长度的文件头和接着的变长度空间数据记录组成。
文件头由100字节的说明信息组成的(附表1),主要说明文件的长度、Shape类型、整个Shape 图层的范围等等,这些信息构成了空间数据的元数据。
在导入空间数据时首先要读入文件头获取Shape文件的基本信息,并以此信息为基础建立相应的元数据表。
而变长度空间数据记录是由固定长度的记录头和变长度记录内容组成,其记录结构基本类似,每条记录都有记录头和记录内容组成(空间坐标对)。
记录头的内容包括记录号(Record Number)和坐标记录长度(Content Length)两个记录项,Shapefile文件中的记录号都是从1开始的,坐标记录长度是按16位字来衡量的。
记录内容包括目标的几何类型(ShapeType)和具体的坐标记录(X,Y),记录内容因要素几何类型的不同,其具体的内容和格式都有所不同。
Shapefile文件

Shapefile文件是美国环境系统研究所(ESRI)所研制的GIS文件系统格式文件,是工业标准的矢量数据文件。
Shapefile将空间特征表中的非拓扑几何对象和属性信息存储在数据集中,特征表中的几何对象存为以坐标点集表示的图形文件—SHP文件,Shapefile文件并不含拓扑(Topological)数据结构。
一个Shape文件包括三个文件:一个主文件(*.shp),一个索引文件(*.shx),和一个dBASE(*.dbf)表。
主文件是一个直接存取,变长度记录的文件,其中每个记录描述构成一个地理特征(Feature)的所有vertices坐标值。
在索引文件中,每条记录包含对应主文件记录距离主文件头开始的偏移量,dBASE表包含SHP文件中每一个Feature的特征属性,表中几何记录和属性数据之间的一一对应关系是基于记录数目的ID。
在dBASE文件中的属性记录必须和主文件中的记录顺序是相同的。
图形数据和属性数据通过索引号建立一一对应的关系。
Shapefile中坐标文件(.shp)由固定长度的文件头和接着的变长度空间数据记录组成。
文件头由100字节的说明信息组成的(附表1),主要说明文件的长度、Shape类型、整个Shape 图层的范围等等,这些信息构成了空间数据的元数据。
在导入空间数据时首先要读入文件头获取Shape文件的基本信息,并以此信息为基础建立相应的元数据表。
而变长度空间数据记录是由固定长度的记录头和变长度记录内容组成,其记录结构基本类似,每条记录都有记录头和记录内容组成(空间坐标对)。
记录头的内容包括记录号(Record Number)和坐标记录长度(Content Length)两个记录项,Shapefile文件中的记录号都是从1开始的,坐标记录长度是按16位字来衡量的。
记录内容包括目标的几何类型(ShapeType)和具体的坐标记录(X,Y),记录内容因要素几何类型的不同,其具体的内容和格式都有所不同。
Shape文件及其编辑

第五章Shape文件及其编辑Shapefile是ArcView存贮空间数据的内部缺省文件格式。
利用这一简单的、非拓仆型的格式,ArcView实现了地理要素属性信息与几何位置的统一贮存,这正是目前大多数GIS 专业软件流行的设计方法。
第一节 Shape格式及其优点ArcView的Shape文件格式可以通过其自身的实用工具创建,也可以经由ARC/INFO coverage或其它的桌面地图格式转换得到。
转换既可以在其它程序中进行(比如:在ARC/INFO中,使用ARCshpe及shpeARC命令便可以实现coverage与shpe文件之间的双向转换),也可以在ArcView界面下来做(先将数据源调入,然后再转换模块将其转换成Shape格式,参见“空间分析”部分)。
以上特点充分反映了ArcView对数据格式的灵活性和高度兼容性。
Shape文件实际上只是ArcView进行GIS数据管理(定义感兴趣的地理要素的几何关系及要素属性)的5种核心文件类型之一。
这5种文件作为项目工作组的内容被贮存于同一个工作目录中,相互以不同的扩展名相区别:.shp——贮存地理要素的几何关系的文件。
..shx——贮存图形要素的几何索引的文件。
.dbf——贮存要素属性信息的dBASE文件。
当Shape文件作为一个主题被加入到视图之后,该.dbf文件将会作为一个要素表格来显示。
.sbn和sbx——贮存地理要素的空间位置索引的文件。
除非您执行过类似于选择“主题之主题”、空间连接等操作,或者您对一个主题(属性表)的Shape字段创建过一个索引,否则这两种格式的文件将不会存在。
另外得注意:如果您在进行各种操作过程中,中途没有对源数据目录进行过类似于“保存项目”之类的“写操作”,那么当您最终关闭ArcView 或当前Project时,程序将会自动剔除这两种邻时文件而使其不能在磁盘中得以永久保存,如果出现这样情况,那么您就无法在当前工作目录下找到它们。
简述创建shapefile的流程

简述创建shapefile的流程
创建 shapefile 文件需要以下步骤:
1. 准备数据:shapefile 文件可以存储点、线、面三种几何类型的数据,需要准备相应格式的数据。
例如,如果要创建一个包含多边形的 shapefile,则需要准备一个包含多边形坐标的表格数据。
2. 选择工具:有多种工具可以创建 shapefile 文件,如 ArcGIS、QGIS 等 GIS 软件,也可以使用 Python 中的`shapely`库或`ogr`库。
3. 打开创建工具并导入数据:根据所选工具的不同,可以通过文件菜单、工具按钮或命令行等方式打开创建 shapefile 的工具,并导入准备好的数据。
4. 设置坐标系:在导入数据之前,需要设置正确的坐标系。
坐标系是用于定义地理空间数据位置的参考系统,如果坐标系不正确,可能会导致数据位置偏差。
5. 定义几何类型:根据导入的数据类型,选择要创建的几何类型(点、线、面)。
6. 保存 shapefile:完成上述步骤后,就可以将数据保存为 shapefile 文件了。
在保存时,需要指定文件名、保存路径和文件格式(.shp、.dbf、.shx、.prj 等)。
需要注意的是,在创建 shapefile 文件时,需要确保数据的准确性和完整性,否则可能会导致数据无法正常显示或分析。
另外,如果需要在不同的 GIS 软件中使用 shapefile 文件,需要注意坐标系的一致性。
shapefile包的用法 -回复

shapefile包的用法-回复Shapefile是一种经常被使用的地理信息系统(GIS)文件格式,它可以用来存储和传输地理位置、空间对象和属性信息。
它由几个文件组成,包括.shp、.shx、.dbf和.prj文件。
这篇文章将一步一步地介绍如何使用shapefile包来读取、创建和编辑shapefile文件。
第一步:安装shapefile包首先,在Python环境中安装shapefile包。
可以使用以下命令在终端或命令提示符中安装:pip install pyshp安装完成后,你就可以在Python程序中使用shapefile包了。
第二步:导入shapefile包在Python程序的开头,导入shapefile包:pythonimport shapefile第三步:读取shapefile文件使用以下代码来读取一个shapefile文件:pythonsf = shapefile.Reader("path/to/shapefile.shp")这将创建一个`ShapefileReader`对象,用于读取shapefile文件的几何形状、属性以及其他相关信息。
第四步:访问shapefile的几何对象和属性可以使用以下代码来访问shapefile中的几何对象和属性:pythonshapes = sf.shapes() # 获取所有几何形状records = sf.records() # 获取所有属性记录通过迭代遍历这些几何形状和属性记录,可以获取每个几何形状的坐标和每个属性记录的值:pythonfor shape in shapes:points = shape.points # 获取几何形状的坐标列表# 处理坐标数据...for record in records:attributes = record.attributes # 获取属性记录的值# 处理属性数据...第五步:创建shapefile文件shapefile包还允许创建新的shapefile文件并添加几何对象和属性。
ArcGIS三大文件格式解析

ArcGIS三⼤⽂件格式解析原⽂⼀、Shape数据Shapefile是ArcView GIS 3.x的原⽣数据格式,属于简单要素类,⽤点、线、多边形存储要素的形状,却不能存储拓扑关系,具有简单、快速显⽰的优点。
⼀个shapefile是由若⼲个⽂件组成的,空间信息和属性信息分离存储,所以称之为“基于⽂件”。
每个shapefile,都⾄少有这三个⽂件组成,其中:*.shp 存储的是⼏何要素的的空间信息,也就是XY坐标*.shx存储的是有关*.shp存储的索引信息。
它记录了在*.shp中,空间数据是如何存储的,XY坐标的输⼊点在哪⾥,有多少XY坐标对等信息*.dbf存储地理数据的属性信息的dBase表这三个⽂件是⼀个shapefile的基本⽂件,shapefile还可以有其他⼀些⽂件,但所有这些⽂件都与该shapefile同名,并且存储在同⼀路径下。
其它较为常见的⽂件:*.prj 如果shapefile定义了坐标系统,那么它的空间参考信息将会存储在*.prj⽂件中*.shp.xml 这是对shapefile进⾏元数据浏览后⽣成的xml元数据⽂件*.sbn和*.sbx 这两个存储的是shapefile的空间索引,它能加速空间数据的读取。
这两个⽂件是在对数据进⾏操作、浏览或连接后才产⽣的,也可以通过ArcToolbox >Data Management Tools >Indexes >Add spatial Index⼯具⽣成。
当使⽤ArcCatalog对shapefile进⾏创建、移动、删除或重命名等操作,或使⽤ArcMap对shapefile进⾏编辑时,ArcCatalog将⾃动维护数据的完整性,将所有⽂件同步改变。
所以需要使⽤ArcCatalog管理shapefile。
虽然Shapefile⽆法存储拓扑关系,但它并不是普通⽤于显⽰的图形⽂件,作为地理数据,它⾃⾝有拓扑的。
⽐如⼀个多边形要素类,shapefile会按顺时针⽅向为它的所有顶点排序,然后按顶点顺序两两连接成的边线向量,在向量右侧的为多边形的内部,在向量左侧的是多边形的外部。
shp文件格式

Shapefile shape format ( .shp )The main file ( .shp ) contains the primary geographic reference data in the shapefile. The file consists of a single fixed length header followed by one or more variable length records. Each of the variable length records includes a record header component and a record contents component. A detailed description of the file format is given in the Esri Shapefile Technical Description . [ 1 ] This format should not be confused with the AutoCAD shape font source format, which shares the .shp extension.The main file header is fixed at 100 bytes in length and contains 17 fields; nine 4-byte (32-bit signed integer or int32) integer fields followed by eight 8-byte ( double ) signed floating point fields:Bytes Type EndiannessUsage 0–3 int32 bigFile code (always hex value 0x0000270a) 4–23 int32 bigUnused; five uint32 24–27int32 big File length (in 16-bit words, including the header) 28–31int32 little Version 32–35int32 little Shape type (see reference below) 36–67 doublelittle Minimum bounding rectangle (MBR) of all shapes contained within the shapefile; four doubles in the following order: min X, min Y, max X, max Y 68–83 doublelittle Range of Z; two doubles in the following order: min Z, max Z 84–99 double little Range of M; two doubles in the following order: min M, max MThe file then contains any number of variable-length records. Each record is prefixed with a record-header of 8 bytes:Bytes Type Endianness Usage0–3 int32 big Record number (1-based)4–7 int32 big Record length (in 16-bit words)Following the record header is the actual record:Bytes Type Endianness Usage0–3 int32 little Shape type (see reference below)4–- - Shape contentThe variable length record contents depend on the shape type. The following are the possible shape types:Value Shape type Fields0 Null shape None1 Point X, Y3 Polyline MBR, Number of parts, Number of points, Parts, Points5 Polygon MBR, Number of parts, Number of points, Parts, Points8 MultiPoint MBR, Number of points, Points11 PointZ X, Y, Z, MMandatory : MBR, Number of parts, Number of points, Parts, Points, Z range, Z array13 PolylineZOptional : M range, M arrayMandatory : MBR, Number of parts, Number of points, Parts, Points, Z range, Z array15 PolygonZOptional : M range, M arrayMandatory : MBR, Number of points, Points, Z range, Z array18 MultiPointZOptional : M range, M array21 PointM X, Y, MMandatory : MBR, Number of parts, Number of points, Parts, Points23 PolylineMOptional : M range, M arrayMandatory : MBR, Number of parts, Number of points, Parts, Points25 PolygonMOptional : M range, M arrayMandatory : MBR, Number of points, Points28 MultiPointMOptional Fields : M range, M arrayMandatory : MBR, Number of parts, Number of points, Parts, Part types, Points, Z range, Z array 31 MultiPatchOptional : M range, M arrayIn common use, shapefiles containing Point, Polyline, and Polygon are extremely popular. The "Z" types arethree-dimensional. The "M" types contain a user-defined measurement which coincides with the point being referenced. Three-dimensional shapefiles are rather uncommon, and the measurement functionality has been largely superseded by more robust databases used in conjunction with the shapefile data.[ edit ] Shapefile shape index format ( .shx )The shapefile index contains the same 100-byte header as the .shp file, followed by any number of 8-byte fixed-length records which consist of the following two fields:Bytes Type Endianness Usage0–3 int32 big Record offset (in 16-bit words)4–7 int32 big Record length (in 16-bit words)Using this index, it is possible to seek backwards in the shapefile by seeking backwards first in the shape index (which is possible because it uses fixed-length records), reading the record offset, and using that to seek to the correct position in the .shp file. It is also possible to seek forwards an arbitrary number of records by using the same method.[ edit ] Shapefile attribute format ( .dbf )Attributes for each shape are stored in the dBase format. An alternative format that can also be used is the xBase format, which has an open specification , and is used in open source Shapefile libraries , such as the Shapefile C library. [ 2]。
Shapefile格式说明

Shapefile格式说明及读写代码示例Shape files 数据说明Shape files 是ESRI 提供的一种矢量数据格式,它没有拓扑信息,一个Shape files 由一组文件组成,其中必要的基本文件包括坐标文件( .shp )、索引文件( .shx )和属性文件( .dbf )三个文件。
坐标文件的结构说明坐标文件(.shp) 用于记录空间坐标信息。
它由头文件和实体信息两部分构成(如图 2.1 所示)。
坐标文件的文件头坐标文件的文件头是一个长度固定(100 bytes) 的记录段,一共有9 个int 型和7 个double 型数据,主要记录内容见表2.2 。
图2.1 坐标文件的结构表2.2shapefiles 头文件表注:最后4 个加星号特别标示的四个数据只有当这个Shapefile 文件包含Z 方向坐标或者具有Measure 值时才有值,否则为0.0各种数据,例如权值、道路长度等信息。
位序细心的读者会注意到表2.2 中的数值的位序有Little 和big 的区别,对于位序是big 的数据我们在读取时要小心。
通常,数据的位序都是Little ,但在有些情况下可能会是big ,二者的区别在于它们位序的顺序相反。
一个位序为big 的数据,如果我们想得到它的真实数值,需要将它的位序转换成Little 即可。
转换原理非常简单,就是交换字节顺序,下面是作者实现的在两者间进行转换的程序,代码如下:// 位序转换程序unsigned long OnChangeByteOrder (int indata){char ss[8];char ee[8];unsigned long val = unsigned long(indata);_ultoa( val, ss, 16 );// 将十六进制的数(val) 转到一个字符串(ss) 中int i;int length=strlen(ss);if(length!=8){for(i=0;i<8-length;i++)ee[i]='0';for(i=0;i<length;i++)ee[i+8-length]=ss[i];for(i=0;i<8;i++)ss[i]=ee[i];}////****** 进行倒序int t;t =ss[0];ss[0] =ss[6];ss[6] =t;t =ss[1];ss[1] =ss[7];ss[7] =t;t =ss[2];ss[2] =ss[4];ss[4] =t;t =ss[3];ss[3] =ss[5];ss[5] =t;////******//****** 将存有十六进制数(val) 的字符串(ss) 中的十六进制数转成十进制数int value=0;for(i=0;i<8;i++){int k;CString mass;mass=ss[i];if(ss[i]=='a' ||ss[i]=='b' ||ss[i]=='c' ||ss[i]=='d' ||ss[i]=='e' ||ss[i]=='f')k=10+ss[i]-'a';elsesscanf(mass,"%d",&k);value=value+int(k*pow(16,7-i));}return (value);}Shapefile 文件支持的几何类型(ShapeType )Shapefile 文件所支持的几何类型如表2.3 所示:表2.3shapefiles 文件支持的几何类型对于一个不是记录Null Shape 类型的Shapefile 文件,它所记录的空间目标的几何类型必须一致,不能在一个Shapefile 文件中同时记录两种不同类型的几何目标。
qgis shapefile裁剪

qgis shapefile裁剪QGIS(Quantum GIS)是一款开源的地理信息系统(GIS)软件,广泛应用于地理数据分析、地图制作和空间数据处理。
Shapefile是一种常见的矢量地理数据格式,适用于存储空间数据。
本文将介绍如何在QGIS中使用Shapefile进行裁剪操作,以及实际应用场景。
一、QGIS软件简介QGIS是一款功能强大的GIS软件,具有丰富的地理数据处理、分析、可视化等功能。
在QGIS中,用户可以轻松地对矢量数据进行操作,包括导入、导出、裁剪等。
接下来,我们将重点介绍如何对Shapefile格式数据进行裁剪。
二、Shapefile格式概述Shapefile是一种常见的矢量数据格式,由ESRI公司开发。
它包含三个主要文件:.shp(几何图形)、.shx(索引)和.dbf(属性数据)。
Shapefile文件易于传输和分享,但在处理大量数据时,文件体积可能会较大。
三、QGIS中Shapefile的裁剪操作步骤在QGIS中,对Shapefile进行裁剪的操作步骤如下:1.打开QGIS软件,导入需要裁剪的Shapefile数据。
2.创建一个矩形或多边形区域,作为裁剪的区域。
可以使用QGIS工具栏中的“绘制工具”或直接在图层上绘制。
3.选中裁剪区域,点击“处理”菜单中的“裁剪”(或使用快捷键Ctrl+Y)。
4.等待裁剪操作完成。
完成后,查看结果图层,确认裁剪效果。
四、裁剪实例与应用以下是一个实际应用场景:假设我们需要获取某城市范围内的POI(兴趣点)数据,可以将整个城市的Shapefile数据导入QGIS,然后绘制一个城市边界多边形。
接着对整个POI 图层进行裁剪,从而获取城市范围内的POI数据。
五、总结与建议通过对QGIS中Shapefile裁剪操作的学习,我们可以更加高效地处理和分析地理数据。
在实际应用中,根据需求选择合适的裁剪区域,可以为我们提供更有针对性的数据支持。
qgis shapefile裁剪

qgis shapefile裁剪摘要:1.简介2.QGIS Shapefile 裁剪的原理3.使用QGIS 进行Shapefile 裁剪的步骤4.裁剪后的数据处理与分析5.总结正文:1.简介QGIS 是一款开源的地理信息系统软件,广泛应用于地图制作、数据分析等领域。
在QGIS 中,Shapefile 是一种常见的地理数据格式。
裁剪是GIS 中常用的操作之一,通过裁剪,我们可以提取出感兴趣区域的数据,便于进一步的处理与分析。
本文将详细介绍如何在QGIS 中进行Shapefile 裁剪操作。
2.QGIS Shapefile 裁剪的原理QGIS Shapefile 裁剪操作是基于矢量数据进行的。
首先,我们需要将要裁剪的Shapefile 数据加载到QGIS 中,然后设置裁剪的边界(通常是一个矩形框或者一个图层),最后执行裁剪操作。
QGIS 会根据裁剪边界与Shapefile 数据进行空间关系判断,提取出满足条件的数据。
3.使用QGIS 进行Shapefile 裁剪的步骤(1)安装与启动QGIS首先,需要在电脑上安装QGIS 软件。
安装完成后,运行QGIS,进入主界面。
(2)加载Shapefile 数据在QGIS 主界面中,点击“图层”->“添加图层”,选择要裁剪的Shapefile 文件,点击“确定”加载数据。
(3)设置裁剪边界在QGIS 主界面中,点击“工具”->“地理处理”->“裁剪”,在弹出的对话框中,选择要裁剪的图层,设置裁剪后的图层名称,点击“确定”执行裁剪操作。
(4)裁剪后的数据处理与分析裁剪完成后,新产生的图层将包含裁剪后的数据。
可以对裁剪后的数据进行进一步的处理与分析,例如导出为新的Shapefile 文件、进行空间分析等。
4.裁剪后的数据处理与分析裁剪后的数据可以根据实际需求进行处理与分析。
例如,可以导出裁剪后的数据为新的Shapefile 文件,以便在其他软件中使用;可以对裁剪后的数据进行空间分析,如缓冲区分析、叠加分析等。
SHAPE存储格式说明

ESRI Shapefile Technical Description An ESRI White Paper—July 1998Copyright © 1997, 1998 Environmental Systems Research Institute, Inc.All rights reserved.Printed in the United States of America.The information contained in this document is the exclusive property of Environmental Systems Research Institute, Inc. This work is protected under United States copyright law and other international copyright treaties and conventions. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying and recording, or by any information storage or retrieval system, except as expressly permitted in writing by Environmental Systems Research Institute, Inc. All requests should be sent to Attention: Contracts Manager, Environmental Systems Research Institute, Inc., 380 New York Street, Redlands, CA 92373-8100 USA.The information contained in this document is subject to change without notice.U.S. GOVERNMENT RESTRICTED/LIMITED RIGHTSAny software, documentation, and/or data delivered hereunder is subject to the terms of the License Agreement. In no event shall the Government acquire greater than RESTRICTED/LIMITED RIGHTS. At a minimum, use, duplication, or disclosure by the Government is subject to restrictions as set forth in FAR §52.227-14 Alternates I, II, and III (JUN 1987); FAR §52.227-19 (JUN 1987) and/or FAR §12.211/12.212 (Commercial Technical Data/Computer Software); and DFARS §252.227-7015 (NOV 1995) (Technical Data) and/or DFARS §227.7202 (Computer Software), as applicable. Contractor/Manufacturer is Environmental Systems Research Institute, Inc., 380 New York Street, Redlands, CA 92373- 8100 USA.In the United States and in some countries, ARC/INFO, ArcCAD, ArcView, ESRI, and PC ARC/INFO are registered trademarks; 3D Analyst, ADF, AML, ARC COGO, ARC GRID, ARC NETWORK, ARC News, ARC TIN, ARC/INFO, ARC/INFO LIBRARIAN, ARC/INFO—Professional GIS, ARC/INFO—The World's GIS, ArcAtlas, ArcBrowser, ArcCAD, ArcCensus, ArcCity, ArcDoc, ARCEDIT, ArcExplorer, ArcExpress, ARCPLOT, ArcPress, ArcScan, ArcScene, ArcSchool, ArcSdl, ARCSHELL, ArcStorm, ArcTools, ArcUSA, ArcUser, ArcView, ArcWorld, Atlas GIS, AtlasWare, Avenue, Business MAP, DAK, DATABASE INTEGRATOR, DBI Kit, ESRI, ESRI—Team GIS, ESRI—The GIS People, FormEdit, Geographic Design System, GIS by ESRI, GIS for Everyone, GISData Server, IMAGE INTEGRATOR, Insite MAP, MapCafé, MapObjects, NetEngine, PC ARC/INFO, PC ARCEDIT, PC ARCPLOT,PC ARCSHELL, PC DATA CONVERSION, PC NETWORK, PC OVERLAY, PC STARTER KIT, PC TABLES, SDE, SML, Spatial Database Engine, StreetMap, TABLES, the ARC COGO logo, the ARC GRID logo, the ARC NETWORK logo, the ARC TIN logo, the ARC/INFO logo, the ArcCAD logo, the ArcCAD WorkBench logo, the ArcData emblem, the ArcData logo, the ArcData Online logo, the ARCEDIT logo, the ArcExplorer logo, the ArcExpress logo, the ARCPLOT logo, the ArcPress logo, the ArcPress for ArcView logo, the ArcScan logo, the ArcStorm logo, the ArcTools logo, the ArcView 3D Analyst logo, the ArcView Data Publisher logo, the ArcView GIS logo, the ArcView Internet Map Server logo, the ArcView Network Analyst logo, the ArcView Spatial Analyst logo, the ArcView StreetMap logo, the Atlas GIS logo, the Avenue logo, the Business MAP logo, the Business MAP PRO logo, the Common Design Mark, the DAK logo, the ESRI corporate logo, the ESRI globe logo, the MapCafé logo, the MapObjects logo, the MapObjects Internet Map Server logo, the NetEngine logo, the PC ARC/INFO logo, the SDE logo, the SDE CAD Client logo, The World's Leading Desktop GIS, ViewMaker, Water Writes, and Your Personal Geographic Information System are trademarks; and ArcData, ARCMAIL, ArcOpen, ArcQuest, ArcWatch, ArcWeb, Rent-a-Tech, , and@ are service marks of Environmental Systems Research Institute, Inc.The names of other companies and products herein are trademarks or registered trademarks of their respective trademark owners.J-7855ESRI Shapefile TechnicalDescriptionAn ESRI White PaperContentsWhy Shapefiles?Shapefile Technical Description Organization of the Main File Main File Record Contents Organization of the Index File Organization of the dBASE File GlossaryESRI White Paper Page 1 2 2 5 232526iJ-7855ESRI Shapefile TechnicalDescriptionThis document defines the shapefile (.shp) spatial data format anddescribes why shapefiles are important. It lists the tools available inEnvironmental Systems Research Institute, Inc. (ESRI), software forcreating shapefiles directly or converting data into shapefiles from otherformats. This document also provides all the technical informationnecessary for writing a computer program to create shapefiles without theuse of ESRI® software for organizations that want to write their own datatranslators.Why Shapefiles?How Shapefiles Can Be Created A shapefile stores nontopological geometry and attribute information for the spatial features in a data set. The geometry for a feature is stored as a shape comprising a set of vector coordinates.Because shapefiles do not have the processing overhead of a topological data structure, they have advantages over other data sources such as faster drawing speed and edit ability. Shapefiles handle single features that overlap or that are noncontiguous. They also typically require less disk space and are easier to read and write.Shapefiles can support point, line, and area features. Area features are represented as closed loop, double-digitized polygons. Attributes are held in a dBASE® format file. Each attribute record has a one-to-one relationship with the associated shape record.Shapefiles can be created with the following four general methods:Export Shapefiles can be created by exporting any data source to a shapefile using ARC/INFO®, PC ARC/INFO®, Spatial Database Engine™ (SDE™), ArcView® GIS, or Business MAP™ software.Digitize Shapefiles can be created directly by digitizing shapes using ArcView GIS feature creation tools.Programming Using Avenue™ (ArcView GIS), MapObjects™, ARC MacroLanguage (AML™) (ARC/INFO), or Simple Macro Language (SML™)(PC ARC/INFO) software, you can create shapefiles within your programs.Write directly to the shapefile specifications by creating a program.ESRI White PaperAny floating point number smaller than –10 is considered by a shapefile reader to ESRI Shapefile Technical DescriptionJ-7855 SDE, ARC/INFO, PC ARC/INFO, Data Automation Kit (DAK ™), and ArcCAD ® software provide shape-to-coverage data translators, and ARC/INFO also provides a coverage-to-shape translator. For exchange with other data formats, the shapefile specifications are published in this paper. Other data streams, such as those from global positioning system (GPS) receivers, can also be stored as shapefiles or X,Y event tables. Shapefile Technical DescriptionNaming ConventionsExamplesNumeric TypesOrganization of the Main File Computer programs can be created to read or write shapefiles using the technical specification in this section. An ESRI shapefile consists of a main file, an index file, and a dBASE table. The main file is a direct access, variable-record-length file in which each record describes a shape with a list of its vertices. In the index file, each record contains the offset of the corresponding main file record from the beginning of the main file. The dBASE table contains feature attributes with one record per feature. The one-to-one relationship between geometry and attributes is based on record number. Attribute records in the dBASE file must be in the same order as records in the main file. All file names adhere to the 8.3 naming convention. The main file, the index file, and the dBASE file have the same prefix. The prefix must start with an alphanumeric character (a –Z, 0–9), followed by zero or up to seven characters (a –Z, 0–9, _, -). The suffix for the main file is .shp. The suffix for the index file is .shx. The suffix for the dBASE table is .dbf. All letters in a file name are in lower case on operating systems with case sensitive file names. Main file: counties.shp Index file: counties.shx dBASE table: counties.dbf A shapefile stores integer and double-precision numbers. The remainder of this document will refer to the following types: Integer: Signed 32-bit integer (4 bytes) Double: Signed 64-bit IEEE double-precision floating point number (8 bytes) Floating point numbers must be numeric values. Positive infinity, negative infinity, and Not-a-Number (NaN) values are not allowed in shapefiles. Nevertheless, shapefiles support the concept of "no data" values, but they are currently used only for measures. 38 represent a "no data" value. The first section below describes the general structure and organization of the shapefile. The second section describes the record contents for each type of shape supported in the shapefile. The main file (.shp) contains a fixed-length file header followed by variable-lengthrecords. Each variable-length record is made up of a fixed-length record header followed by variable-length record contents. Figure 1 illustrates the main file organization.March 1998 2ESRI Shapefile Technical Description J-7855Figure 1 Organization of the Main File ... ... Byte OrderAll the contents in a shapefile can be divided into two categories: Data related ∙ ∙ Main file record contents Main file header’s data description fields (Shape Type, Bounding Box, etc.) File management related ∙ ∙ File and record lengths Record offsets, and so on The integers and double-precision integers that make up the data description fields in the file header (identified below) and record contents in the main file are in little endian (PC or Intel ®) byte order. The integers and double-precision floating point numbers that make up the rest of the file and file management are in big endian (Sun ® or Motorola ®) byte order. The Main File Header The main file header is 100 bytes long. Table 1 shows the fields in the file header withtheir byte position, value, type, and byte order. In the table, position is with respect to the start of the file.ESRI White Paper 3ESRI Shapefile Technical DescriptionJ-7855Table 1Description of the Main File HeaderBytePosition Byte 0 Byte 4 Byte 8 Byte 12 Byte 16 Byte 20 Byte 24 Byte 28 Byte 32 Byte 36 Byte 44 Byte 52 Byte 60 Byte 68* Byte 76* Byte 84* Byte 92* FieldFile CodeUnusedUnusedUnusedUnusedUnusedFile LengthVersionShape TypeBounding BoxBounding BoxBounding BoxBounding BoxBounding BoxBounding BoxBounding BoxBounding BoxValue9994File Length1000Shape TypeXminYminXmaxYmaxZminZmaxMminMmaxTypeIntegerIntegerIntegerIntegerIntegerIntegerIntegerIntegerIntegerDoubleDoubleDoubleDoubleDoubleDoubleDoubleDoubleOrderBigBigBigBigBigBigBigLittleLittleLittleLittleLittleLittleLittleLittleLittleLittle* Unused, with value 0.0, if not Measured or Z typeThe value for file length is the total length of the file in 16-bit words (including the fifty 16-bit words that make up the header).All the non-Null shapes in a shapefile are required to be of the same shape type. The values for shape type are as follows:Value1358111315182123252831 March 1998 Shape TypeNull ShapePointPolyLinePolygonMultiPointPointZPolyLineZPolygonZMultiPointZPointMPolyLineMPolygonMMultiPointMMultiPatch4ESRI Shapefile Technical Description J-7855Shape types not specified above (2, 4, 6, etc., and up to 33) are reserved for future use. Currently, shapefiles are restricted to contain the same type of shape as specified above. In the future, shapefiles may be allowed to contain more than one shape type. If mixed shape types are implemented, the shape type field in the header will flag the file as such. The Bounding Box in the main file header stores the actual extent of the shapes in the file: the minimum bounding rectangle orthogonal to the X and Y (and potentially the M and Z) axes that contains all shapes. If the shapefile is empty (that is, has no records), the values for Xmin, Ymin, Xmax, and Ymax are unspecified. Mmin and Mmax can contain "no data" values (see Numeric Types on page 2) for shapefiles of measured shape types that contain no measures. Record HeadersThe header for each record stores the record number and content length for the record. Record headers have a fixed length of 8 bytes. Table 2 shows the fields in the file header with their byte position, value, type, and byte order. In the table, position is with respect to the start of the record. Table 2 Description of Main File Record Headers Record numbers begin at 1. The content length for a record is the length of the record contents section measured in 16-bit words. Each record, therefore, contributes (4 + content length) 16-bit words toward the total length of the file, as stored at Byte 24 in the file header. Main FileRecord Contents Null Shapes Shapefile record contents consist of a shape type followed by the geometric data for the shape. The length of the record contents depends on the number of parts and vertices in a shape. For each shape type, we first describe the shape and then its mapping to record contents on disk. In Tables 3 through 16, position is with respect to the start of the record contents. A shape type of 0 indicates a null shape, with no geometric data for the shape. Eachfeature type (point, line, polygon, etc.) supports nulls it is valid to have points and null points in the same shapefile. Often null shapes are place holders; they are used during shapefile creation and are populated with geometric data soon after they are created.ESRI White Paper 5ESRI Shapefile Technical DescriptionJ-7855Table 3Null Shape Record Contents Shape Types in X,Y SpacePointA point consists of a pair of double-precision coordinates in the order X,Y. Point { Double Double X Y // X coordinate // Y coordinate } Table 4 Point Record Contents MultiPoint A MultiPoint represents a set of points, as follows:MultiPoint{Double[4] Integer Point[NumPoints] Box NumPoints Points // Bounding Box // Number of Points // The Points in the Set }The Bounding Box is stored in the order Xmin, Ymin, Xmax, Ymax. March 1998 6ESRI Shapefile Technical DescriptionJ-7855Table 5MultiPoint Record ContentsPolyLineA PolyLine is an ordered set of vertices that consists of one or more parts. A part is a connected sequence of two or more points. Parts may or may not be connected to one another. Parts may or may not intersect one another.Because this specification does not forbid consecutive points with identical coordinates, shapefile readers must handle such cases. On the other hand, the degenerate, zero length parts that might result are not allowed.PolyLine {Double[4] Integer IntegerInteger[NumParts] Point[NumPoints] BoxNumParts NumPoints Parts Points // Bounding Box // Number of Parts// Total Number of Points // Index to First Point in Part // Points for All Parts}The fields for a PolyLine are described in detail below: BoxNumPartsThe Bounding Box for the PolyLine stored in the order Xmin, Ymin, Xmax, Ymax.The number of parts in the PolyLine.NumPoints The total number of points for all parts. PartsPointsAn array of length NumParts. Stores, for each PolyLine, the index of its first point in the points array. Array indexes are with respect to 0.An array of length NumPoints. The points for each part in the PolyLine are stored end to end. The points for Part 2 follow the points for Part 1, and so on. The parts array holds the array index of the starting point for each part. There is no delimiter in the points array between parts.ESRI White Paper7ESRI Shapefile Technical DescriptionJ-7855Table 6PolyLine Record ContentsPolygonA polygon consists of one or more rings. A ring is a connected sequence of four or more points that form a closed, non-self-intersecting loop. A polygon may contain multiple outer rings. The order of vertices or orientation for a ring indicates which side of the ring is the interior of the polygon. The neighborhood to the right of an observer walking along the ring in vertex order is the neighborhood inside the polygon. Vertices of rings defining holes in polygons are in a counterclockwise direction. Vertices for a single, ringedpolygon are, therefore, always in clockwise order. The rings of a polygon are referred to as its parts.Because this specification does not forbid consecutive points with identical coordinates, shapefile readers must handle such cases. On the other hand, the degenerate, zero length or zero area parts that might result are not allowed.The Polygon structure is identical to the PolyLine structure, as follows:Polygon {Double[4] Integer IntegerInteger[NumParts] Point[NumPoints] BoxNumParts NumPoints Parts Points // Bounding Box // Number of Parts// Total Number of Points // Index to First Point in Part // Points for All Parts}The fields for a polygon are described in detail below: BoxNumPartsThe Bounding Box for the polygon stored in the order Xmin, Ymin, Xmax, Ymax.The number of rings in the polygon.NumPoints The total number of points for all rings.March 19988ESRI Shapefile Technical Description J-7855Parts Points An array of length NumParts. Stores, for each ring, the index of its first point in the points array. Array indexes are with respect to 0.An array of length NumPoints. The points for each ring in the polygon are stored end to end. The points for Ring 2 follow the points for Ring 1, and so on. The parts array holds the array index of the starting point for each ring. There is no delimiter in the points array between rings.The instance diagram in Figure 2 illustrates the representation of polygons. This figure shows a polygon with one hole and a total of eight vertices.The following are important notes about Polygon shapes.The rings are closed (the first and last vertex of a ring MUST be the same).The order of rings in the points array is not significant.Polygons stored in a shapefile must be clean. A clean polygon is one that1.2. Has no self-intersections. This means that a segment belonging to one ring may not intersect a segment belonging to another ring. The rings of a polygon can touch each other at vertices but not along segments. Colinear segments are considered intersecting.Has the inside of the polygon on the "correct" side of the line that defines it. The neighborhood to the right of an observer walking along the ring in vertex order is the inside of the polygon. Vertices for a single, ringed polygon are, therefore, always in clockwise order. Rings defining holes in these polygons have a counterclockwise orientation. "Dirty" polygons occur when the rings that define holes in the polygon also go clockwise, which causes overlapping interiors.Figure 2An Example Polygon Instancev1v4 v2v3ESRI White Paper 9ESRI Shapefile Technical DescriptionJ-7855For this example, NumParts equals 2 and NumPoints equals 10. Note that the order of the points for the donut (hole) polygon are reversed below.5Parts:123456789 Points:Table 7Polygon Record ContentsMeasured Shape Types inX,Y SpacePointMShapes of this type have an additional coordinate M. Note that "no data" value can be specified as a value for M (see Numeric Types on page 2).A PointM consists of a pair of double-precision coordinates in the order X, Y, plus a measure M.PointM {Double Double Double }March 1998X Y M // X coordinate // Y coordinate // Measure10ESRI Shapefile Technical DescriptionJ-7855Table 8PointM Record ContentsMultiPointMA MultiPointM represents a set of PointMs, as followsMultiPointM {Double[4] IntegerPoint[NumPoints] Double[2]Double[NumPoints] BoxNumPoints Points M Range M Array // Bounding Box // Number of Points // The Points in the Set// Bounding Measure Range // Measures}The fields for a MultiPointM are BoxThe Bounding Box for the MultiPointM stored in the order Xmin, Ymin, Xmax, YmaxNumPoints The number of Points Points M RangeM ArrayAn array of Points of length NumPointsThe minimum and maximum measures for the MultiPointM stored in the order Mmin, MmaxAn array of measures of length NumPointsESRI White Paper11ESRI Shapefile Technical DescriptionJ-7855Table 9MultiPointM Record ContentsPolyLineMA shapefile PolyLineM consists of one or more parts. A part is a connected sequence of two or more points. Parts may or may not be connected to one another. Parts may or may not intersect one another.PolyLineM {Double[4] Integer IntegerInteger[NumParts] Point[NumPoints] Double[2]Double[NumPoints] }The fields for a PolyLineM are BoxNumParts NumPoints Parts Points M Range M Array // Bounding Box // Number of Parts// Total Number of Points // Index to First Point in Part // Points for All Parts// Bounding Measure Range // Measures for All PointsBoxNumPartsThe Bounding Box for the PolyLineM stored in the order Xmin, Ymin, Xmax, Ymax.The number of parts in the PolyLineM.NumPoints The total number of points for all parts. PartsPointsMarch 1998An array of length NumParts. Stores, for each part, the index of its first point in the points array. Array indexes are with respect to 0.An array of length NumPoints. The points for each part in the PolyLineM are stored end to end. The points for Part 2 follow the points for Part 1, and so on. The parts array holds the array index of the starting point for each part. There is no delimiter in the points array between parts.12ESRI Shapefile Technical DescriptionJ-7855M RangeM ArrayThe minimum and maximum measures for the PolyLineM stored in the order Mmin, Mmax.An array of length NumPoints. The measures for each part in the PolyLineM are stored end to end. The measures for Part 2 follow themeasures for Part 1, and so on. The parts array holds the array index of the starting point for each part. There is no delimiter in the measure array between parts.Table 10PolyLineM Record ContentsPolygonMA PolygonM consists of a number of rings. A ring is a closed, non-self-intersecting loop. Note that intersections are calculated in X,Y space, not in X,Y,M space. A PolygonM may contain multiple outer rings. The rings of a PolygonM are referred to as its parts.The PolygonM structure is identical to the PolyLineM structure, as follows:PolygonM {Double[4] Integer IntegerInteger[NumParts] Point[NumPoints] Double[2]Double[NumPoints] }ESRI White PaperBoxNumParts NumPoints Parts Points M Range M Array // Bounding Box // Number of Parts// Total Number of Points // Index to First Point in Part // Points for All Parts// Bounding Measure Range // Measures for All Points13ESRI Shapefile Technical DescriptionJ-7855The fields for a PolygonM areBox NumParts The Bounding Box for the PolygonM stored in the order Xmin, Ymin, Xmax, Ymax.The number of rings in the PolygonM.NumPoints The total number of points for all rings.Parts PointsM Range M Array An array of length NumParts. Stores, for each ring, the index of its first point in the points array. Array indexes are with respect to 0.An array of length NumPoints. The points for each ring in the PolygonM are stored end to end. The points for Ring 2 follow the points for Ring 1, and so on. The parts array holds the array index of the starting point for each ring. There is no delimiter in the points array between rings.The minimum and maximum measures for the PolygonM stored in the order Mmin, Mmax.An array of length NumPoints. The measures for each ring in the PolygonM are stored end to end. The measures for Ring 2 follow the measures for Ring 1, and so on. The parts array holds the array index of the starting measure for each ring. There is no delimiter in the measure array between rings.The following are important notes about PolygonM shapes.The rings are closed (the first and last vertex of a ring MUST be the same).The order of rings in the points array is not significant.March 1998 14ESRI Shapefile Technical DescriptionJ-7855Table 11PolygonM Record ContentsShape Types in X,Y,Z SpacePointZShapes of this type have an optional coordinate M. Note that "no data" value can be specified as a value for M (see Numeric Types on page 2).A PointZ consists of a triplet of double-precision coordinates in the order X, Y, Z plus a measure.PointZ {Double Double Double Double X Y Z M // X coordinate // Y coordinate // Z coordinate // Measure}Table 12PointZ Record ContentsESRI White Paper15ESRI Shapefile Technical DescriptionJ-7855 MultiPointZ A MultiPointZ represents a set of PointZs, as follows:MultiPointZ{Double[4]IntegerPoint[NumPoints] Double[2]Double[NumPoints] Double[2]Double[NumPoints] BoxNumPointsPointsZ RangeZ ArrayM RangeM Array// Bounding Box// Number of Points// The Points in the Set// Bounding Z Range// Z Values// Bounding Measure Range// Measures}The Bounding Box is stored in the order Xmin, Ymin, Xmax, Ymax.The bounding Z Range is stored in the order Zmin, Zmax. Bounding M Range is storedin the order Mmin, Mmax.Table 13MultiPointZ Record ContentsMarch 1998 16ESRI Shapefile Technical Description J-7855PolyLineZ A PolyLineZ consists of one or more parts. A part is a connected sequence of two ormore points. Parts may or may not be connected to one another. Parts may or may notintersect one another.PolyLineZ{Double[4]IntegerIntegerInteger[NumParts] Point[NumPoints] Double[2]Double[NumPoints] Double[2]Double[NumPoints] BoxNumPartsNumPointsPartsPointsZ RangeZ ArrayM RangeM Array// Bounding Box// Number of Parts// Total Number of Points// Index to First Point in Part// Points for All Parts// Bounding Z Range// Z Values for All Points// Bounding Measure Range// Measures}The fields for a PolyLineZ are described in detail below:Box NumParts The Bounding Box for the PolyLineZ stored in the order Xmin, Ymin, Xmax, Ymax.The number of parts in the PolyLineZ.NumPoints The total number of points for all parts.Parts PointsZ Range Z Array M Range M Array An array of length NumParts. Stores, for each part, the index of its first point in the points array. Array indexes are with respect to 0.An array of length NumPoints. The points for each part in the PolyLineZ are stored end to end. The points for Part 2 follow the points for Part 1, and so on. The parts array holds the array index of the starting point for each part. There is no delimiter in the points array between parts.The minimum and maximum Z values for the PolyLineZ stored in the order Zmin, Zmax.An array of length NumPoints. The Z values for each part in the PolyLineZ are stored end to end. The Z values for Part 2 follow the Z values for Part 1, and so on. The parts array holds the array index of the starting point for each part. There is no delimiter in the Z array between parts.The minimum and maximum measures for the PolyLineZ stored in the order Mmin, Mmax.An array of length NumPoints. The measures for each part in the PolyLineZ are stored end to end. The measures for Part 2 follow the measures for PartESRI White Paper 17。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Shape files数据说明Shape files是ESRI提供的一种矢量数据格式,它没有拓扑信息,一个Shape files由一组文件组成,其中必要的基本文件包括坐标文件(.shp)、索引文件(.shx)和属性文件(.dbf)三个文件。
1、坐标文件的结构说明坐标文件(.shp)用于记录空间坐标信息。
它由头文件和实体信息两部分构成(如图 2.1所示)。
1)坐标文件的文件头坐标文件的文件头是一个长度固定(100 bytes)的记录段,一共有9个int型和7个double 型数据,主要记录内容见表2.2。
图2.1坐标文件的结构表2.2shapefiles头文件表注:最后4个加星号特别标示的四个数据只有当这个Shapefile文件包含Z方向坐标或者具有Measure值时才有值,否则为0.0。
所谓Measure值,是用于存储需要的附加数据,可以用来记录各种数据,例如权值、道路长度等信息。
(1)位序细心的读者会注意到表2.2中的数值的位序有Little和big的区别,对于位序是big的数据我们在读取时要小心。
通常,数据的位序都是Little,但在有些情况下可能会是big,二者的区别在于它们位序的顺序相反。
一个位序为big的数据,如果我们想得到它的真实数值,需要将它的位序转换成Little即可。
转换原理非常简单,就是交换字节顺序,下面是作者实现的在两者间进行转换的程序,代码如下://位序转换程序unsigned long OnChangeByteOrder (int indata){char ss[8];char ee[8];unsigned long val = unsigned long(indata);_ultoa( val, ss, 16 );//将十六进制的数(val)转到一个字符串(ss)中int i;int length=strlen(ss);if(length!=8){for(i=0;i<8-length;i++)ee[i]='0';for(i=0;i<length;i++)ee[i+8-length]=ss[i];for(i=0;i<8;i++)ss[i]=ee[i];}////******进行倒序int t;t =ss[0];ss[0] =ss[6];ss[6] =t;t =ss[1];ss[1] =ss[7];ss[7] =t;t =ss[2];ss[2] =ss[4];ss[4] =t;t =ss[3];ss[3] =ss[5];ss[5] =t;////******//******将存有十六进制数(val)的字符串(ss)中的十六进制数转成十进制数int value=0;for(i=0;i<8;i++){int k;CString mass;mass=ss[i];if(ss[i]=='a' ||ss[i]=='b' ||ss[i]=='c' ||ss[i]=='d' ||ss[i]=='e' ||ss[i]=='f')k=10+ss[i]-'a';elsesscanf(mass,"%d",&k);value=value+int(k*pow(16,7-i));}return (value);}(2)Shapefile文件支持的几何类型(ShapeType)Shapefile文件所支持的几何类型如表2.3所示:表2.3shapefiles文件支持的几何类型对于一个不是记录Null Shape 类型的Shapefile文件,它所记录的空间目标的几何类型必须一致,不能在一个Shapefile文件中同时记录两种不同类型的几何目标。
读取坐标文件(.shp)的文件头的代码如下:void OnReadShp(CString ShpFileName){FILE* m_ShpFile_fp; //****Shp文件指针//打开坐标文件if((m_ShpFile_fp=fopen(ShpFileName,"rb"))==NULL){return;}//读取坐标文件头的内容开始int FileCode;int Unused;int FileLength;int Version;int ShapeType;double Xmin;double Ymin;double Xmax;double Ymax;double Zmin;double Zmax;double Mmin;double Mmax;fread(&FileCode, sizeof(int), 1,m_ShpFile_fp);FileCode = OnChangeByteOrder (FileCode);for(i=0;i<5;i++)fread(&Unused,sizeof(int), 1,m_ShpFile_fp);fread(&FileLength, sizeof(int), 1,m_ShpFile_fp);FileLength = OnChangeByteOrder (FileLength);fread(&Version, sizeof(int), 1,m_ShpFile_fp);fread(&ShapeType, sizeof(int), 1,m_ShpFile_fp);fread(&Xmin, sizeof(double),1,m_ShpFile_fp);fread(&Ymin, sizeof(double),1,m_ShpFile_fp);fread(&Xmax, sizeof(double),1,m_ShpFile_fp);fread(&Ymax, sizeof(double),1,m_ShpFile_fp);fread(&Zmin, sizeof(double),1,m_ShpFile_fp);fread(&Zmax, sizeof(double),1,m_ShpFile_fp);fread(&Mmin, sizeof(double),1,m_ShpFile_fp);fread(&Mmax, sizeof(double),1,m_ShpFile_fp);//读取坐标文件头的内容结束//根据几何类型读取实体信息……}2)实体信息的内容实体信息负责记录坐标信息,它以记录段为基本单位,每一个记录段记录一个地理实体目标的坐标信息,每个记录段分为记录头和记录内容两部分。
记录头的内容包括记录号(Record Number)和坐标记录长度(Content Length) 两个记录项。
它们的位序都是big。
记录号(Record Number)和坐标记录长度(Content Length) 两个记录项都是int型,并且shapefile文件中的记录号都是从1开始的。
记录内容包括目标的几何类型(ShapeType)和具体的坐标记录(X、Y) ,记录内容因要素几何类型的不同其具体的内容及格式都有所不同。
下面分别介绍点状目标(Point)、线状目标(PolyLine)和面状目标(Polygon)三种几何类型的.shp文件的记录内容:a. 点状目标shapefile中的点状目标由一对X、Y坐标构成,坐标值为双精度型(double)。
点状目标的记录内容如表2.4:表2.4点状目标的记录内容下面是读取点状目标的记录内容的代码:OnReadPointShp(CString ShpFileName){//打开坐标文件……//读取坐标文件头的内容开始……//读取点状目标的实体信息int RecordNumber;int ContentLength;int num =0;while((fread(&RecordNumber, sizeof(int), 1,ShpFile_fp)!=0)){num++;fread(&ContentLength,sizeof(int), 1,ShpFile_fp);RecordNumber = OnChangeByteOrder (RecordNumber);ContentLength= OnChangeByteOrder (ContentLength);int shapeType;double x;double y;fread(&shapeType, sizeof(int), 1,ShpFile_fp);fread(&x, sizeof(double), 1,ShpFile_fp);fread(&y, sizeof(double), 1,ShpFile_fp);}}b. 线状目标shapefile中的线状目标是由一系列点坐标串构成,一个线目标可能包括多个子线段,子线段之间可以是相离的,同时子线段之间也可以相交。
Shapefile允许出现多个坐标完全相同的连续点,当读取文件时一定要注意这种情况,但是不允许出现某个退化的、长度为0的子线段出现。
线状目标的记录内容如表2.5:表2.5线状目标的记录内容具体的数据结构如下:PolyLine{Double[4] Box // 当前线状目标的坐标范围Integer NumParts // 当前线目标所包含的子线段的个数Integer NumPoints // 当前线目标所包含的顶点个数Integer[NumParts] Parts // 每个子线段的第一个坐标点在Points的位置Point[NumPoints] Points // 记录所有坐标点的数组}这些记录项的具体含义如下:Box记录了当前的线目标的坐标范围,它是一个double型的数组,按照Xmin、Ymin、Xmax、Ymax的顺序记录了坐标范围;NumParts记录了当前线目标所包含的子线段的个数;NumPoints记录了当前线目标的坐标点总数;Parts记录了每个子线段的第一个坐标点在坐标数组points中的位置,以便读取数据;Points是用于存放当前线目标的X、Y坐标的数组。
下面是读取线状目标的记录内容的代码:OnReadLineShp(CString ShpFileName){//打开坐标文件……//读取坐标文件头的内容开始……//读取线状目标的实体信息int RecordNumber;int ContentLength;int num =0;while((fread(&RecordNumber, sizeof(int), 1,ShpFile_fp)!=0)){fread(&ContentLength,sizeof(int), 1,ShpFile_fp);RecordNumber = OnChangeByteOrder (RecordNumber);ContentLength= OnChangeByteOrder (ContentLength);int shapeType;double Box[4];int NumParts;int NumPoints;int *Parts;fread(&shapeType, sizeof(int), 1,ShpFile_fp);//读Boxfor(i=0;i<4;i++)fread(Box+i, sizeof(double),1,ShpFile_fp);//读NumParts和NumPointsfread(&NumParts, sizeof(int), 1,ShpFile_fp);fread(&NumPoints, sizeof(int), 1,ShpFile_fp);//读Parts和PointsParts =new int[NumParts];for(i=0;i<NumParts;i++)fread(Parts+i, sizeof(int), 1,ShpFile_fp);int pointNum;for(i=0;i<NumParts;i++){if(i!=NumParts-1)pointNum =Parts[i+1]-Parts[i];elsepointNum =NumPoints-Parts[i];double *PointsX;double *PointsY;PointsX =new double[pointNum];PointsY =new double[pointNum];for(j=0;j<pointNum;j++){fread(PointsX+j, sizeof(double),1,ShpFile_fp);fread(PointsY+j, sizeof(double),1,ShpFile_fp);}delete[] PointsX;delete[] PointsY;}delete[] Parts;}}c. 面状目标shapefile中的面状目标是由多个子环构成,每个子环是由至少四个顶点构成的封闭的、无自相交现象的环。