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
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
def show_data(lyr): """ 显示数据,提供图层参数 :param lyr: :return: """ vp = VectorPlotter(False) vp.plot(lyr, 'bo') vp.draw()
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
######################## 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() # Draw the Asian countries in yellow and print out a few features. vp.plot(lyr, 'y') pb.print_attributes(lyr, 4, ['name'], geom=False) # You can still get a feature that is not in Asia by using its FID. lyr.GetFeature(2).GetField('name') # Set a new filter that selects South American countries and show the results
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()
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()
# 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') ######################### Esri file geodatabase ##############################
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 # falls within New Orleans city boundaries. intersection = marsh_geom.Intersection(nola_geom) vp.plot(intersection, 'yellow', hatch='x') # Figure out how much of New Orleans is wetlands. Throw out lakes and anything # not in the vicinity of New Orleans, and then loop through the remaining water
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()
# 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()
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') web_mercator_sr = osr.SpatialReference()
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()
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()
def show_line(self, line): vp = VectorPlotter(False) vp.plot(line, 'b-') vp.draw()
def show_polygon(self, polygon): vp = VectorPlotter(False) vp.plot(polygon, fill=False, edgecolor='blue') vp.draw()
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()
# 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 ##################################### # Get the shapefile layers in a folder.
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')
def show_point(self, point): vp = VectorPlotter(False) vp.plot(point, 'bo') vp.draw()
######################### 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)
# 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') ds = ogr.Open(fn)
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.') # Use the Germany polygon to set a spatial filter and draw the result as blue # circles. city_lyr.SetSpatialFilter(germany) city_lyr.GetFeatureCount() vp.plot(city_lyr, 'bo')
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)
######################### 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 ############################## # Open the large_cities data source.