标签: shapefile

将新的列属性添加到 shapefile 并使用 Geotools Java 将其保存到数据库

我正在通过添加新的列属性来转换 shapefile。由于此任务是使用 Java 执行的,因此我目前知道的唯一选择是使用 Geotools。我有两个主要问题:

1. 我不知道如何实际添加新的列变量。feature.setAttribute("col","value") 是答案吗?

我从这篇文章中看到的只是示例:https : //gis.stackexchange.com/questions/215660/modifying-feature-attributes-of-a-shapefile-in-geotools,但我没有得到解决方案。

   //Upload the ShapeFile
    File file = JFileDataStoreChooser.showOpenFile("shp", null);
    Map<String, Object> params = new HashMap<>();
    params.put("url", file.toURI().toURL());

    DataStore store = DataStoreFinder.getDataStore(params);
    SimpleFeatureSource featureSource = store.getFeatureSource(store.getTypeNames()[0]);
    String typeName = store.getTypeNames()[0];


     FeatureSource<SimpleFeatureType, SimpleFeature> source =
            store.getFeatureSource(typeName);
        Filter filter = Filter.INCLUDE;

    FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
    try (FeatureIterator<SimpleFeature> features = collection.features()) {
        while (features.hasNext()) {
            SimpleFeature feature = features.next();

            //adding new columns

            feature.setAttribute("ShapeID", "SHP1213");
            feature.setAttribute("UserName", "John");

            System.out.print(feature.getID());
            System.out.print(":");
            System.out.println(feature.getDefaultGeometryProperty().getValue());
        }
    } …
Run Code Online (Sandbox Code Playgroud)

java attributes postgis shapefile geotools

4
推荐指数
1
解决办法
958
查看次数

从边界框坐标列表创建形状文件

关于这个主题的现有问题已经很少,但不幸的是我没有找到可以解决我的问题的东西。

我有一个点 Lat,Long 坐标,即 Lat= 10 和 Long = 10。我想围绕这个点创建一个 0.5 度边界框的形状文件,所以边界框应该如下:

  1. 最小多头 = 9.75
  2. 最小纬度 = 9.75
  3. 最大多头 = 10.25
  4. 最大纬度 = 10.25

有谁知道如何在 Python 中做到这一点?

python gis shapefile

4
推荐指数
2
解决办法
3650
查看次数

UnicodeDecodeError 'utf-8' 编解码器无法解码 - 使用 python shapefile 阅读器

我正在尝试读取 shapefile

r = shapefile.Reader(filepath, encoding = "utf-8")
Run Code Online (Sandbox Code Playgroud)

但是当我尝试从.records()对象获取值时,例如:

 r.records()[0]
Run Code Online (Sandbox Code Playgroud)

它返回给我以下错误:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 4: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)

python unicode shapefile

4
推荐指数
1
解决办法
1524
查看次数

无法使用以下驱动程序打开数据源“”

我试图确保我正确安装了 geodjango,以便我可以按照本教程进行操作

我使用 Brew 作为 Geodjango 先决条件的安装程序:postgresql、postgis、gdal、libgeoip。

我的 gdal 版本是最新的:3.1.2。但是,当我尝试使用ogrinfo world/data/TM_WORLD_BORDERS-0.3.shp检查 .shp 文件时,它会在控制台中返回此错误:

Unable to open datasource '[this is the name of the file here'] with the following drivers.然后它只列出一大堆驱动程序。

我一生都不知道该怎么做,类似问题的所有其他答案都建议升级 GDAL,但我的是最新版本......

django console shapefile gdal geodjango

4
推荐指数
1
解决办法
5958
查看次数

如何在ggplot2中制作连续图例?

我对 ggplot2 中的图例几乎没有疑问。如何从所附代码中连续制作图例。在此代码中,图例采用离散形式,我想将其设为连续形式(平滑连续模式)。代码是可重现的(有点长,但可能在此代码中一行会发生变化

 library(sf)
 library(sp)

 r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

 # Let's mock with a shapefile
 poly <- st_as_sfc(st_bbox(st_as_sf(rasterToPolygons(r))))

 # Sample 4 points
 set.seed(3456)

 sample <- st_sample(poly, 4)
 sample <- st_buffer(sample, c(0.01, 0.02, 0.03))
 sample <- st_sf(x=1:4, sample)
 st_write(sample, "1aa.shp", append = FALSE)
 # Mocked data

 # Now let's start with code -------

 r <- raster(t((volcano[,ncol(volcano):1] - 94) * 4.95))

 # Use sf!!
 pg <- st_read("1aa.shp") # loadshapfile 
 plot(r)
 plot(st_geometry(pg), add= TRUE,) # it appears here …
Run Code Online (Sandbox Code Playgroud)

r raster legend shapefile ggplot2

4
推荐指数
1
解决办法
1141
查看次数

SHX文件如何工作?

如果您有以ESRI形状存储的地理数据,则至少有三个文件:一个以包含vector-data的.shp结尾,一个以包含属性的.dbf结尾,另一个以包含索引的.shx结尾的文件.

我对shx文件感兴趣.它是如何工作的?它是否包含完整的映射,例如"每个几何体的第一个几何图谱映射到dbf中的第三行,第二个几何图谱映射到第一行"?或者它的工作方式不同?

gis shapefile

3
推荐指数
1
解决办法
4477
查看次数

Python:找到一种计算多边形"内部质心"(X,Y)的方法

我有一个多边形(在Shapely对象中转换).我的目标是在图例子后面计算"内部质心"(也称为"表面上的点")(返回x,y值)和"质心"(返回x,y值):

在此输入图像描述

from shapely.geometry import Polygon

ref_polygon = Polygon(points)
# get the x and y coordinate of the centroid
ref_polygon.centroid.wkt
'POINT (558768.9293489187300000 6361851.0362532493000000)'
Run Code Online (Sandbox Code Playgroud)

我的问题是一些程序员已经在Python中开发了一个函数来计算内部质心或知道一些模块来做到这一点.

提前致谢

使用的点(多边形的顶点)是:

points = [(560036.4495758876, 6362071.890493258),
          (560036.4495758876, 6362070.890493258),
          (560036.9495758876, 6362070.890493258),
          (560036.9495758876, 6362070.390493258),
          (560037.4495758876, 6362070.390493258),
          (560037.4495758876, 6362064.890493258),
          (560036.4495758876, 6362064.890493258),
          (560036.4495758876, 6362063.390493258),
          (560035.4495758876, 6362063.390493258),
          (560035.4495758876, 6362062.390493258),
          (560034.9495758876, 6362062.390493258),
          (560034.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.390493258),
          (560032.9495758876, 6362061.890493258),
          (560030.4495758876, 6362061.890493258),
          (560030.4495758876, 6362061.390493258),
          (560029.9495758876, 6362061.390493258),
          (560029.9495758876, 6362060.390493258),
          (560029.4495758876, 6362060.390493258),
          (560029.4495758876, 6362059.890493258),
          (560028.9495758876, 6362059.890493258),
          (560028.9495758876, 6362059.390493258),
          (560028.4495758876, 6362059.390493258),
          (560028.4495758876, 6362058.890493258),
          (560027.4495758876, 6362058.890493258),
          (560027.4495758876, …
Run Code Online (Sandbox Code Playgroud)

python geometry geolocation shapefile

3
推荐指数
1
解决办法
2591
查看次数

使用ggplot绘制SpatialPolygonDataFrame

我正在为大伦敦地区提供一个形状文件.我使用包中的readShapePoly函数将maptools其作为一个加载到R中SpatialPolygonDataFrame.

我想绘制那些多边形..我已经通过使用plotR中的函数基本已经完成了.输出看起来如下图所示:

在此输入图像描述

现在,我正在尝试绘制相同的形状文件,ggplot2但它不适用于我.我在图中看到一些奇怪的线,如图所示: 在此输入图像描述

我使用的代码是:

london.wards <- readShapePoly("~/TD/london_wards2013/london_wards2013.shp"
                          , proj4string=CRS(projString))
wards.count <- nrow(london.wards@data)
# assign id for each lsoa

london.wards@data$id <- 1:wards.count
wards.fort <- fortify(london.wards, region='id')
ggplot(wards.fort, aes(long, lat)) + geom_polygon(colour='black', fill='white')
Run Code Online (Sandbox Code Playgroud)

其中projString是描述用于输入形状文件的投影的投影字符串.

gis r shapefile ggplot2 r-maptools

3
推荐指数
2
解决办法
3129
查看次数

显示一个Shapefile

我有一个shapefile要显示。我尝试使用matplotlib来显示它,但是我得到了: 我得到什么 但是,当我尝试使用在线网站进行展示时,我得到了; 我想要的是

如何获得第二张图像?

这是我的代码:

import shapefile
import matplotlib.pyplot as plt

print("Initializing Shapefile")
sf = shapefile.Reader("ap_abl")
apShapes = sf.shapes()
points = apShapes[3].points
print("Shapefile Initialized")

print("Initializing Display")
fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlim([78, 79])
plt.ylim([19, 20])
print("Display Initialized")

print("Creating Polygon")
ap = plt.Polygon(points, fill=False, edgecolor="k")
ax.add_patch(ap)
print("Polygon Created")

print("Displaying polygon")
plt.show()
Run Code Online (Sandbox Code Playgroud)

先感谢您。

python matplotlib shapefile python-3.x

3
推荐指数
2
解决办法
6035
查看次数

图例问题以及图例范围和颜色分布

我在情节和图例上的正确颜色范围有问题。

这是我使用的代码:

data.ch4 <- read.csv2("v42_CH4_1970_TOT.txt",skip = 3,stringsAsFactors = FALSE, header = F)
num_data <- data.frame(data.matrix(data.ch4))

library(maptools)
library(lattice)
library(png)

#map loading
map1 <- readShapePoly("CNTR_2014_03M_SH/Data/CNTR_RG_03M_2014.shp")
coordinates(num_data) <- ~V2+V1  
gridded(num_data) <- TRUE

#plotting
png(file="Map2.png",width=35,height=30,unit="cm", res=200, type = "cairo")

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
             sp.layout = list("sp.polygons",map1),contour=F)
dev.off()
Run Code Online (Sandbox Code Playgroud)

这是带有数据的文件:https : //www.sendspace.com/file/hjtatp (压缩后通常重量为57 mb)

此处开始的地图(但地图具有第二优先级,可以跳过)

无需修改任何比例,外观如下: 在此处输入图片说明

所以一切都是蓝色的。显然,从最小到最大值都有很大的距离。我想固定比例,例如最后一个值应为“高于x”。我试图这样做:

在此处输入图片说明

所以现在看起来好多了。这是我的方法:

#Fixed breakpoints (?)
at <- c(0e+0, 1.5e-5, 1.0e-4, 1.0e-3, 1.0e-2, 1.0e-1, 1.0e+0, 2.0e+0, 1.0e+1, 1.0e+2, 2.0e+2,5.0e+2)

spplot(num_data["V3"], xlim=c(-5,35), ylim=c(35,70),
             sp.layout = list("sp.polygons",map1),
             contour=F,
             at=at) #right there …
Run Code Online (Sandbox Code Playgroud)

r spatial shapefile r-maptools r-sp

3
推荐指数
1
解决办法
3762
查看次数