Пример #1
0
def polygonReading(fn, pg_lyrName_r):
    ds = ogr.Open(fn, 0)  #0为只读模式,1为编辑模式
    if ds is None:
        sys.exit('Could not open{0}'.format(fn))
    pg_lyr = ds.GetLayer(pg_lyrName_r)  #可以直接数据层(文件)名或者指定索引

    vp = VectorPlotter(True)  #显示vector数据
    vp.plot(pg_lyr)

    pg_schema = pg_lyr.schema  #查看属性表字段名和类型
    for field in pg_schema:
        print(field.name, field.GetTypeName())

    i = 0
    for feat in pg_lyr:  #循环feature
        print(
            ".................................................................."
        )
        atts = feat.items()
        print(atts)
        pg = feat.geometry()  #获取feature的几何对象
        #        ring=
        name = feat.GetField('NAME')  #读取feature的属性
        Shape_Area = feat.GetField('Shape_Area')
        print(name, Shape_Area, '\n', pg, '\n')

        for j in range(pg.GetGeometryCount()):  #循环几何对象获取ring,获取顶点坐标
            ring = pg.GetGeometryRef(j)
            for coordi in ring.GetPoints():
                print(coordi)
        i += 1
        if i == 12:
            break
    del ds
Пример #2
0
def lineReading(fn, ln_lyrName_r):
    ds = ogr.Open(fn, 0)  #0为只读模式,1为编辑模式
    if ds is None:
        sys.exit('Could not open{0}'.format(fn))
    ln_lyr = ds.GetLayer(ln_lyrName_r)  #可以直接数据层(文件)名或者指定索引

    vp = VectorPlotter(True)  #显示vector数据
    vp.plot(ln_lyr, 'bo')

    ln_schema = ln_lyr.schema  #查看属性表字段名和类型
    for field in ln_schema:
        print(field.name, field.GetTypeName())

    i = 0
    for feat in ln_lyr:  #循环feature
        ln = feat.geometry()  #获取feature的几何对象
        name = feat.GetField('name')  #读取feature的属性
        Shape_Leng = feat.GetField('Shape_Leng')
        print(name, Shape_Leng, '\n', ln, '\n', ln.GetPointCount())
        for j in range(ln.GetPointCount()):  #循环几何对象(线)d的vertex顶点
            if j < 6:
                print((i, ln.GetX(i), ln.GetY(i)))  #只能通过GetX()和GetY()的方法获取顶点坐标
        i += 1
        if i == 12:
            break
    del ds
Пример #3
0
def pointReading(fn, pt_lyrName_r):
    ds = ogr.Open(fn, 0)  #0为只读模式,1为编辑模式
    if ds is None:
        sys.exit('Could not open{0}'.format(fn))
    pt_lyr = ds.GetLayer(pt_lyrName_r)  #可以直接数据层(文件)名或者指定索引
    vp = VectorPlotter(True)  #显示vector数据
    vp.plot(pt_lyr, 'bo')

    i = 0
    for feat in pt_lyr:  #循环feature
        pt = feat.geometry()
        pt_x = pt.GetX()
        pt_y = pt.GetY()
        name = feat.GetField('NAME')
        kind = feat.GetField('KIND')
        print(name, kind, (
            pt_x,
            pt_y,
        ))
        i += 1
        if i == 12:
            break
    del ds
Пример #4
0
def show_data(lyr):
    """
    显示数据,提供图层参数
    :param lyr: 
    :return: 
    """
    vp = VectorPlotter(False)
    vp.plot(lyr, 'bo')
    vp.draw()
Пример #5
0
def main(sample_shp, img_shp, description=None):
    if description == None:
        sys.exit("缺少‘描述’ 程序不能运行!")
    vp = VectorPlotter(False)
    # 打开样本矢量
    sample_ds = ogr.Open(sample_shp, 1)
    sample_lyr = sample_ds.GetLayer(0)
    # 打开影像矢量
    image_ds = ogr.Open(img_shp)
    img_lyr = image_ds.GetLayer(0)
    # 过滤描述信息
    # sample_lyr.SetAttributeFilter('描述 = {}'.format(description))
    sample_lyr.ResetReading()
    sample_lyr.SetAttributeFilter("despict = {}".format("'" + description + "'"))
    count = 1
    total = sample_lyr.GetFeatureCount()
    for feat in sample_lyr:
        geom = feat.geometry().Clone()
        # 对影像矢量进行筛选
        img_lyr.ResetReading()
        img_lyr.SetSpatialFilter(geom)
        imgforfeat = []
        for img_feat in img_lyr:
            # 获取属性
            img_name = img_feat.GetField('browsefile')
            img_names = os.path.splitext(os.path.basename(img_name))[0].split("_")
            name_id = "_".join([img_names[0], img_names[4], img_names[5]])
            imgforfeat.append(name_id)
        img_lyr.SetSpatialFilter(None)
        str_name = "/".join(imgforfeat)
        feat.SetField('image', str_name)
        sample_lyr.SetFeature(feat)
        progress(count / total)
        count += 1
    sample_lyr.SetAttributeFilter(None)
    return None
Пример #6
0
from osgeo import ogr
from matplotlib import pyplot as plt
from ospybook.vectorplotter import VectorPlotter

ring = ogr.Geometry(ogr.wkbLinearRing)
g = ogr.Geometry(ogr.wkbMultiPolygon)
ring.AddPoint(45, 78)
ring.AddPoint(23, 78)
ring.AddPoint(12, 56)
ring.AddPoint(56, 24)
polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(ring)
polygon.CloseRings()
g.AddGeometry(polygon)
vp = VectorPlotter(True)
ring = polygon.GetGeometryRef(0)
for i in range(ring.GetPointCount()):
    ring.SetPoint(i, ring.GetX(i) - 5, ring.GetY(i))
g.AddGeometry(polygon)
vp.plot(g, name='Polygon')

plt.ioff()
plt.show()
Пример #7
0
pb.print_attributes(fn, 3, ['NAME', 'POP_MAX'])

# Turn off geometries but skip field list parameters that come before the
# "geom" one.
#pb.print_attributes(fn, 3, geom=False)

# If you want to see what happens without the "geom" keyword in the last
# example, try this:
#pb.print_attributes(fn, 3, False)

# Import VectorPlotter and change directories
from ospybook.vectorplotter import VectorPlotter
os.chdir(os.path.join(data_dir, 'global'))

# Plot populated places on top of countries from an interactive session.
vp = VectorPlotter(True)
vp.plot('ne_50m_admin_0_countries.shp', fill=False)
vp.plot('ne_50m_populated_places.shp', 'bo')

# Plot populated places on top of countries non-interactively. Delete the vp
# variable if you tried the interactive one first.
del vp
vp = VectorPlotter(False)
vp.plot('ne_50m_admin_0_countries.shp', fill=False)
vp.plot('ne_50m_populated_places.shp', 'bo')
vp.draw()

#########################  3.4 Getting metadata  ##############################

# Open the large_cities data source.
fn = os.path.join(data_dir, 'Washington', 'large_cities.geojson')
Пример #8
0
# Set this variable to your osgeopy-data directory so that the following
# examples will work without editing. We'll use the os.path.join() function
# to combine this directory and the filenames to make a complete path. Of
# course, you can type the full path to the file for each example if you'd
# prefer.
data_dir = r'D:\osgeopy-data'
# data_dir =



##########################  7.1  Overlay tools  ###############################

# Look at New Orleans wetlands. First get a specific marsh feature near New
# Orleans.
vp = VectorPlotter(True)
water_ds = ogr.Open(os.path.join(data_dir, 'US', 'wtrbdyp010.shp'))
water_lyr = water_ds.GetLayer(0)
water_lyr.SetAttributeFilter('WaterbdyID = 1011327')
marsh_feat = water_lyr.GetNextFeature()
marsh_geom = marsh_feat.geometry().Clone()
vp.plot(marsh_geom, 'b')

# Get the New Orleans boundary.
nola_ds = ogr.Open(os.path.join(data_dir, 'Louisiana', 'NOLA.shp'))
nola_lyr = nola_ds.GetLayer(0)
nola_feat = nola_lyr.GetNextFeature()
nola_geom = nola_feat.geometry().Clone()
vp.plot(nola_geom, fill=False, ec='red', ls='dashed', lw=3)

# Intersect the marsh and boundary polygons to get the part of the marsh that
Пример #9
0
 def show_point(self, point):
     vp = VectorPlotter(False)
     vp.plot(point, 'bo')
     vp.draw()
Пример #10
0

# Try out the function.
print_layers(os.path.join(data_dir, 'Washington', 'large_cities.geojson'))

#########################  4.2.1 SpatiaLite  ##################################

# Use the function in ospybook to look at a SpatiaLite database.
pb.print_layers(os.path.join(data_dir, 'global', 'natural_earth_50m.sqlite'))

# Get the populated_places layer.
ds = ogr.Open(os.path.join(data_dir, 'global', 'natural_earth_50m.sqlite'))
lyr = ds.GetLayer('populated_places')

# Plot the populated places layer in the SpatiaList database interactively.
vp = VectorPlotter(True)
vp.plot(lyr, 'bo')

# Plot the populated places layer in the SpatiaList database non-interactively.
vp = VectorPlotter(False)
vp.plot(lyr, 'bo')
vp.draw()

#########################  4.2.2 PostGIS  #####################################

# Print out layers in a PostGIS database. This will not work for you unless
# you set up a PostGIS server.
pb.print_layers('PG:user=chris password=mypass dbname=geodata')

#########################  4.2.3 Folders  #####################################
Пример #11
0
 def show_polygon(self, polygon):
     vp = VectorPlotter(False)
     vp.plot(polygon, fill=False, edgecolor='blue')
     vp.draw()
Пример #12
0
# trying this example.
out_fn = os.path.join(data_dir, 'output', 'testdata.shp')

# Create an empty shapefile that uses a UTM SRS. If you run this it will
# create the shapefile with a .prj file containing the SRS info.
sr = osr.SpatialReference()
sr.ImportFromEPSG(26912)
ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource(out_fn)
lyr = ds.CreateLayer('counties', sr, ogr.wkbPolygon)


#########################  8.2.4 Projecting geometries  #######################

# Get the world landmasses and plot them.
world = pb.get_shp_geom(os.path.join(data_dir, 'global', 'ne_110m_land_1p.shp'))
vp = VectorPlotter(True)
vp.plot(world)

# Create a point for the Eiffel Tower.
tower = ogr.Geometry(wkt='POINT (2.294694 48.858093)')
tower.AssignSpatialReference(osr.SpatialReference(osr.SRS_WKT_WGS84))

# Try to reproject the world polygon to Web Mercator. This should spit out
# an error.
web_mercator_sr = osr.SpatialReference()
web_mercator_sr.ImportFromEPSG(3857)
world.TransformTo(web_mercator_sr)

# Set a config variable and try the projection again.
from osgeo import gdal
gdal.SetConfigOption('OGR_ENABLE_PARTIAL_REPROJECTION', 'TRUE')
Пример #13
0
    provence = sd.GetLayer(0)
    provence.SetAttributeFilter('area>16000')
    city = provence.GetNextFeature()
    city_geo = city.geometry().Clone()
    print(city.GetField('long'))
    vp.plot(city_geo, 'b')
    vp.draw()
    return city_geo


def get_intersect():
    highway = ogr.Open(r'G:\arcgis data\argis数据\12月16日数据\test_intersect.shp')
    way = highway.GetLayer(0)
    wayfeature = way.GetNextFeature()
    waygeo = wayfeature.geometry().Clone()
    vp.plot(waygeo, 'g')
    vp.draw()
    return waygeo


if __name__ == '__main__':
    """
    1.通过两个图层获取两个要素
    2.通过要素的geometry().Clone()方法获取几何对象
    3.进行相交,获取相交后的几何对象"""
    vp = VectorPlotter(False)
    citygeo = get_city()
    highgeo = get_intersect()
    insect = citygeo.Intersection(highgeo)
    vp.plot(insect, 'yellow')
    vp.draw()
Пример #14
0
json_lyr.CreateField(pop_fld)

feat_defn = json_lyr.GetLayerDefn()

# Create an output feature to use repeatedly
json_feat = ogr.Feature(feat_defn)

for shp_feat in shp_lyr:
    if shp_feat.GetField('CONTINENT') == 'Africa':

        # Copy geometry and attribute values if in Africa
        name = shp_feat.GetField('NAME')
        pop = shp_feat.GetField('POP_EST')
        json_feat.SetField('Name', name)
        json_feat.SetField('Population', pop)
        json_feat.SetGeometry(shp_feat.geometry())

        # Insert the data into the GeoJSON file
        json_lyr.CreateFeature(json_feat)

# Delete the datasource. Python will write it to disk and close the file when
# the variable isn't needed anymore.
del json_ds

# Visualize the output
pb.print_attributes(json_fn)
vp = VectorPlotter(False)
vp.plot(shp_fn, fc='0.9')
vp.plot(json_fn, 'y')
vp.draw()
Пример #15
0
from osgeo import ogr
from ospybook.vectorplotter import VectorPlotter
from matplotlib import pyplot as plt

Point = ogr.Geometry(ogr.wkbPoint)
Line = ogr.Geometry(ogr.wkbLineString)
Line.AddPoint(12,34)
Line.AddPoint(15,56)
Line.AddPoint(34,37)
Point.AddPoint(12,34)
BufferDistance = 50
Poly1 = Point.Buffer(BufferDistance)
Poly2 = Line.Buffer(10)
vp = VectorPlotter(True)
vp.plot(Poly1,symbol='b')
vp.plot(Poly2,symbol='r')
plt.ioff()
plt.show()
Пример #16
0
 def show_line(self, line):
     vp = VectorPlotter(False)
     vp.plot(line, 'b-')
     vp.draw()
Пример #17
0
from osgeo import ogr
from matplotlib import pyplot as plt
from ospybook.vectorplotter import VectorPlotter
line = ogr.Geometry(ogr.wkbLineString)
line.AddPoint(1111.7655,65432.88)
line.AddPoint(2244.544,87654.86)
line.AddPoint(3216.877,5434.097)
line.AddPoint(5444.766,6559.98)
print(line.ExportToWkt())
vp = VectorPlotter(True)
vp.plot(line,symbol='yellow')
plt.ioff()
plt.show()
Пример #18
0
# examples will work without editing. We'll use the os.path.join() function
# to combine this directory and the filenames to make a complete path. Of
# course, you can type the full path to the file for each example if you'd
# prefer.
# data_dir = r'D:\osgeopy-data'
data_dir =


#########################  4.3 Viewing your data  ##############################

# Set a filename to use for the next several examples.
fn = os.path.join(data_dir, 'Washington', 'large_cities.geojson')

# Print name and population attributes.
import ospybook as pb
pb.print_attributes(fn, fields=['NAME', 'POPULATION'])

# Import VectorPlotter
from ospybook.vectorplotter import VectorPlotter

# Plot large_cities on top of counties from an interactive session.
vp = VectorPlotter(True)
vp.plot(os.path.join(data_dir, 'Washington', 'counties.shp'), fill=False)
vp.plot(os.path.join(data_dir, 'Washington', 'large_cities.geojson'), 'bo', ms=8)

# Plot big_cities on top of counties non-interactively.
vp = VectorPlotter(False)
vp.plot(os.path.join(data_dir, 'Washington', 'counties.shp'), fill=False)
vp.plot(os.path.join(data_dir, 'Washington', 'large_cities.geojson'), 'bo', ms=8)
vp.draw()
Пример #19
0
print "Tipo geometria features {0}".format(lyr.GetGeomType())
feat = lyr.GetFeature(0)
print "Tipo geometria feature zero {0}".format(feat.geometry().GetGeometryName())
print "Spatial Reference {0}".format(lyr.GetSpatialRef())

#Name and type of field
for field in lyr.schema:
    print field.name, field.GetTypeName()

i = 0
for feat in lyr:
    pt = feat.geometry()
    x = pt.GetX()
    y = pt.GetY()
    name = feat.GetField('NAME')
    pop = feat.GetField('POP_MAX')
#    print(name, pop, x, y)
    i += 1
    if i == 10:
        break

del ds

from ospybook.vectorplotter import VectorPlotter
vp = VectorPlotter(True)
vp.plot(fn, 'bo')
vp.show('test')



Пример #20
0
from osgeo import ogr
from matplotlib import pyplot as plt
from ospybook.vectorplotter import VectorPlotter
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(11.34, 63.09)
print(point.ExportToWkt())
x = point.GetX()
y = point.GetY()
vp = VectorPlotter(True)
vp.plot(point, 'rs')
plt.title('CreatePoint')
plt.ioff()
plt.show()
Пример #21
0
#########################  6.2  Working with points  ##########################

###########################  6.2.1  Single points  ############################

# Create the firepit point.
firepit = ogr.Geometry(ogr.wkbPoint)
firepit.AddPoint(59.5, 11.5)

# Try out GetX and GetY.
x, y = firepit.GetX(), firepit.GetY()
print('{}, {}'.format(x, y))

# Take a look at the point.
print(firepit)
vp = VectorPlotter(True)
vp.plot(firepit, 'bo')

# Edit the point coordinates.
firepit.AddPoint(59.5, 13)
vp.plot(firepit, 'rs')
print(firepit)

# Or edit the point using SetPoint instead of AddPoint.
firepit.SetPoint(0, 59.5, 13)
print(firepit)

# Make a 2.5D point.
firepit = ogr.Geometry(ogr.wkbPoint25D)
firepit.AddPoint(59.5, 11.5, 2)
print(firepit)
Пример #22
0
def main(sample_shp1, out_shp):
    vp = VectorPlotter(False)
    water_ds = ogr.Open(sample_shp1)
    water_lyr = water_ds.GetLayer(0)
    # 准备输出的shp
    out_shp_ds = water_ds.GetDriver().CreateDataSource(out_shp)
    out_lyr = out_shp_ds.CreateLayer('category', srs=water_lyr.GetSpatialRef(), geom_type=water_lyr.GetGeomType())
    # 写入属性字段
    out_lyr.CreateFields(water_lyr.schema)
    # 新增字段
    coor_fld = ogr.FieldDefn('相交面', ogr.OFTString)
    coor_fld.SetWidth(50)
    out_lyr.CreateField(coor_fld)
    coor_fld.SetName("总品类")
    out_lyr.CreateField(coor_fld)
    # 创建初始要素
    out_defn = out_lyr.GetLayerDefn()
    out_feat = ogr.Feature(out_defn)

    mem_dri = ogr.GetDriverByName('Memory')
    mem_ds = mem_dri.CreateDataSource(' ')
    temp_lyr = mem_ds.CopyLayer(water_lyr, 'temp_lyr')
    water_lyr.ResetReading()
    for feat in water_lyr:
        geometry = feat.geometry().Clone()
        temp_lyr.SetSpatialFilter(geometry)
        for freq in range(temp_lyr.GetFeatureCount() - 1):
            pass







        # 拷贝feat
        category = new_feat.GetField('地类')
        objectid = new_feat.GetField('OBJECTID')
        for i in range(new_feat.GetFieldCount()):
            value = feat.GetField(i)
            out_feat.SetField(i, value)
        out_feat.SetField(out_feat.GetFieldCount() - 2, objectid)
        out_feat.SetField(out_feat.GetFieldCount() - 1, category)
        out_feat.SetGeometry(geometry)
        out_lyr.CreateFeature(out_feat)
        objectids = []
        categorys = []
        intersection_geom = []
        intersection_geom_temp = []
        objectids.append(str(objectid))
        categorys.append(category)
        intersection_geom_temp.append(geometry.Clone())

        for igeom in intersection_geom:
            for new_feat in temp_lyr:
                if new_feat.GetField('OBJECTID') in objectids:
                    continue
                category = new_feat.GetField('地类')
                objectid = new_feat.GetField('OBJECTID')
                new_geom = new_feat.geometry().Clone()
                intersection = new_geom.Intersection(geometry)
                if intersection.Area() != 0.0:
                    intersection_geom.append(intersection.Clone)
                    for i in range(new_feat.GetFieldCount()):
                        value = feat.GetField(i)
                        out_feat.SetField(i, value)
                    if category not in categorys:
                        categorys.append(category)
                    if objectid not in objectids:
                        objectids.append(str(objectid))
                    objectids.sort()
                    all_category = '/'.join(categorys)
                    all_objectid = '/'.join(objectids)

                    geometry = intersection.Clone()
                    out_feat.SetField(out_feat.GetFieldCount() - 2, all_objectid)
                    out_feat.SetField(out_feat.GetFieldCount() - 1, all_category)
                    out_feat.SetGeometry(intersection)
                    out_lyr.CreateFeature(out_feat)
            temp_lyr.SetSpatialFilter(None)
    out_shp_ds.SyncToDisk()
    out_shp_ds = None

    return None
Пример #23
0
# Set a new filter that selects South American countries and show the results
# in blue. The old filter is no longer in effect.
lyr.SetAttributeFilter('continent = "South America"')
vp.plot(lyr, 'b')

# Clear all attribute filters.
lyr.SetAttributeFilter(None)
lyr.GetFeatureCount()



##########################  5.2  Spatial filters  #############################

# Set up an interactive plotter.
vp = VectorPlotter(True)

# Get the Germany polygon. Make sure to plot the full layer before setting the
# filter, or you'll only plot Germany (or you could clear the filter and then
# plot).
ds = ogr.Open(os.path.join(data_dir, 'global'))
country_lyr = ds.GetLayer('ne_50m_admin_0_countries')
vp.plot(country_lyr, fill=False)
country_lyr.SetAttributeFilter('name = "Germany"')
feat = country_lyr.GetNextFeature()
germany = feat.geometry().Clone()

# Plot world cities as yellow dots.
city_lyr = ds.GetLayer('ne_50m_populated_places')
city_lyr.GetFeatureCount()
vp.plot(city_lyr, 'y.')
Пример #24
0
from osgeo import ogr, osr, gdal
import ospybook
from ospybook.vectorplotter import VectorPlotter
import pyproj
vp = VectorPlotter(False)  # 用于显示几何成果

# 创建控件参考对象
# sr = osr.SpatialReference() # 创建一个空的空间参考对象
# sr.ImportFromEPSG() # 使用EPSG码来定义空间参考系统,26912代表NAD83 UTM 12N
# # 使用proj4来定义空间参考系统
# #sr.ImportFromProj4('''+proj=utm +zone=12 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ''')
# print(sr.GetAttrValue('PROJCS'))

# 自定义空间坐标系
# sr = osr.SpatialReference()
# sr.SetProjCS('USER Albers') # 定义投影的名称
# sr.SetWellKnownGeogCS('NAD83') # 设定基准
# sr.SetACEA(29.5, 45.5, 23, -96, 0, 0) # 设定参数:标准平行线1, 标准平行线2, 中央纬线, 中央经线, 东移假定值, 北移假定值
# sr.Fixup() # 该函数用于为缺少的参数添加默认值,并重新排序项目,以使它们与标准匹配
# print(sr.Validate()) # 该函数用于确保没有缺少任何东西,返回一个0表示一切正常

# 几何对象重投影
# file_path = r"C:\Users\think\Desktop\python\python-for-GIS-DATA\osgeopy-data\osgeopy-data\global\ne_110m_land_1p.shp"
# world = ospybook.get_shp_geom(file_path) # 该方法可以从shape file中获得第一个几何对象
# tower = ogr.Geometry(wkt='POINT (2.294694 48.858093)') # 获取艾弗尔铁塔的位置
# # 使用AssignSpatialReference函数为几何对象分配SRS,但是它并不会将数据转换到该坐标系下,仅仅是提供空间信息
# tower.AssignSpatialReference(osr.SpatialReference(osr.SRS_WKT_WGS84)) # 因为WGS84坐标系很常用,所以在OSR模块中有一个常量,
#                                                                       # 用于表示该地理坐标系统的WKT。
# gdal.SetConfigOption('OGR_ENABLE_PARTIAL_REPROJECTION', 'TRUE') # 该方式可以修复,在进行web墨卡托投影时,南北极投影出错的问题
# web_mercator_sr = osr.SpatialReference() # 第一步还是创建空的空间参考对象
# web_mercator_sr.ImportFromEPSG(3857)
Пример #25
0
    shp_row.SetField('timestamp', timestamp)
    shp_lyr.CreateFeature(shp_row)

del csv_ds  # 再使用完CSV数据后将其删除

# 剔除图层中GPS定位错误的点
shp_lyr.SetSpatialFilterRect(-1, -1, 1, 1)
for shp_row in shp_lyr:
    shp_lyr.DeleteFeature(shp_row.GetFID())
shp_lyr.SetSpatialFilter(None)
shp_ds.ExecuteSQL("REPACK " + shp_lyr.GetName())  # 使用REPACK永久的删除错误点
shp_ds.ExecuteSQL("RECOMPUTE EXTENT ON " +
                  shp_lyr.GetName())  # 重新计算shapefile的空间范围

# 绘制结果
vp = VectorPlotter(False)

# vp.plot(shp_lyr, 'r.')


# 定义一个从属性列表中获得唯一值的方法
def get_unique(datasource, layer_name, field_name):
    sql = "SELECT DISTINCT {0} FROM {1}".format(field_name, layer_name)
    lyr = datasource.ExecuteSQL(sql)
    values = []
    for row in lyr:
        values.append(row.GetField(field_name))
    datasource.ReleaseResultSet(lyr)
    return values

Пример #26
0
# Try out the function.
print_layers(os.path.join(data_dir, 'Washington', 'large_cities.geojson'))


#########################  SpatiaLite  #########################################

# Use the function in ospybook to look at a SpatiaLite database.
pb.print_layers(os.path.join(data_dir, 'global', 'natural_earth_50m.sqlite'))

# Get the populated_places layer.
ds = ogr.Open(os.path.join(data_dir, 'global', 'natural_earth_50m.sqlite'))
lyr = ds.GetLayer('populated_places')

# Plot the populated places layer in the SpatiaList database interactively.
vp = VectorPlotter(True)
vp.plot(lyr)

# Plot the populated places layer in the SpatiaList database non-interactively.
vp = VectorPlotter(False)
vp.plot(lyr)
vp.draw()


#########################  PostGIS  ############################################

# Print out layers in a PostGIS database. This will not work for you unless
# you set up a PostGIS server.
pb.print_layers('PG:user=chris password=mypass dbname=geodata')

Пример #27
0
#########################  6.2  Working with points  ##########################

###########################  6.2.1  Single points  ############################

# Create the firepit point.
firepit = ogr.Geometry(ogr.wkbPoint)
firepit.AddPoint(59.5, 11.5)

# Try out GetX and GetY.
x, y = firepit.GetX(), firepit.GetY()
print('{}, {}'.format(x, y))

# Take a look at the point.
print(firepit)
vp = VectorPlotter(True)
vp.plot(firepit, 'bo')

# Edit the point coordinates.
firepit.AddPoint(59.5, 13)
vp.plot(firepit, 'rs')
print(firepit)

# Or edit the point using SetPoint instead of AddPoint.
firepit.SetPoint(0, 59.5, 13)
print(firepit)

# Make a 2.5D point.
firepit = ogr.Geometry(ogr.wkbPoint25D)
firepit.AddPoint(59.5, 11.5, 2)
print(firepit)
Пример #28
0
box2 = ogr.Geometry(ogr.wkbLinearRing)
box2.AddPoint(89, 23)
box2.AddPoint(92, 23)
box2.AddPoint(92, 22)
box2.AddPoint(89, 22)
garden2 = ogr.Geometry(ogr.wkbPolygon)
garden2.AddGeometry(box2)

# 因为复合多边形是由一个人或多个多边形构成的,所以创建复合多边形向其中添加的是多边形。
gardens = ogr.Geometry(ogr.wkbMultiPolygon)
gardens.AddGeometry(garden1)
gardens.AddGeometry(garden2)
gardens.CloseRings()  # 一次性关闭所有的环
print(gardens)

vp = VectorPlotter(False)
vp.plot(gardens)

# 整体移动复合多边形
for i in range(gardens.GetGeometryCount()):
    ring = gardens.GetGeometryRef(i).GetGeometryRef(0)
    for j in range(ring.GetPointCount()):
        ring.SetPoint(j, ring.GetX(j) + 1, ring.GetY(j) + 0.5)
vp.plot(gardens, fill=False, ec='red', ls='dashed')

# 创建带有岛的多边形
lot = ogr.Geometry(ogr.wkbLinearRing)
lot.AddPoint(58, 38.5)
lot.AddPoint(53, 6)
lot.AddPoint(99.5, 19)
lot.AddPoint(73, 42)
Пример #29
0
# to combine this directory and the filenames to make a complete path. Of
# course, you can type the full path to the file for each example if you'd
# prefer.
# data_dir = r'D:\osgeopy-data'
data_dir =r'C:\Users\westshell_ASUS\Documents\GisData\osgeopy-data'



########################  5.1  Attribute filters  #############################

# Set up an interactive plotter. Because this is the most fun if you do it
# interactively, you'll probably want to do it from the Python interactive
# prompt. If you're going to run it as a script instead, you might want to use
# a non-interactive plotter instead. Just remember to call draw() when you're
# done.
vp = VectorPlotter(True)

# Get the countries shapefile layer
ds = ogr.Open(os.path.join(data_dir, 'global'))
lyr = ds.GetLayer('ne_50m_admin_0_countries')

# Plot the countries with no fill and also print out the first 4 attribute
# records.
vp.plot(lyr, fill=False)
pb.print_attributes(lyr, 4, ['name'], geom=False)

# Apply a filter that finds countries in Asia and see how many records there
# are now.
lyr.SetAttributeFilter('continent = "Asia"')
lyr.GetFeatureCount()
Пример #30
0
    print(feat.GetField('Name'), pt.GetX(), pt.GetY())


#########################  3.3.2 Viewing your data  ###########################

# Print name and population attributes.
import ospybook as pb
fn = os.path.join(data_dir, 'global', 'ne_50m_populated_places.shp')
pb.print_attributes(fn, 3, ['NAME', 'POP_MAX'])

# Import VectorPlotter and change directories
from ospybook.vectorplotter import VectorPlotter
os.chdir(os.path.join(data_dir, 'global'))

# Plot populated places on top of countries from an interactive session.
vp = VectorPlotter(True)
vp.plot('ne_50m_admin_0_countries.shp', fill=False)
vp.plot('ne_50m_populated_places.shp', 'bo')

# Plot populated places on top of countries non-interactively. Delete the vp
# variable if you tried the interactive one first.
del vp
vp = VectorPlotter(False)
vp.plot('ne_50m_admin_0_countries.shp', fill=False)
vp.plot('ne_50m_populated_places.shp', 'bo')
vp.draw()



#########################  3.4 Getting metadata  ##############################
Пример #31
0
# Set this variable to your osgeopy-data directory so that the following
# examples will work without editing. We'll use the os.path.join() function
# to combine this directory and the filenames to make a complete path. Of
# course, you can type the full path to the file for each example if you'd
# prefer.
# data_dir = r'D:\osgeopy-data'
data_dir = r""

########################  5.1  Attribute filters  #############################

# Set up an interactive plotter. Because this is the most fun if you do it
# interactively, you'll probably want to do it from the Python interactive
# prompt. If you're going to run it as a script instead, you might want to use
# a non-interactive plotter instead. Just remember to call draw() when you're
# done.
vp = VectorPlotter(True)

# Get the countries shapefile layer
ds = ogr.Open(os.path.join(data_dir, 'global'))
lyr = ds.GetLayer('ne_50m_admin_0_countries')

# Plot the countries with no fill and also print out the first 4 attribute
# records.
vp.plot(lyr, fill=False)
pb.print_attributes(lyr, 4, ['name'], geom=False)

# Apply a filter that finds countries in Asia and see how many records there
# are now.
lyr.SetAttributeFilter('continent = "Asia"')
lyr.GetFeatureCount()