Exemplo n.º 1
0
def gettingMetadata(fn, lyrName):
    ds = ogr.Open(fn, 0)
    if ds is None:
        sys.exit('Could not open{0}'.format(fn))
    lyr = ds.GetLayer(lyrName)
    print("extent:", lyr.GetExtent())  #(min_x,max_x, min_y, max_y)
    print("GeomType:", lyr.GetGeomType(), "wkbPoint?",
          lyr.GetGeomType() == ogr.wkbPoint)  #返回索引,根据索引查找对应的数据类型
    feat = lyr.GetFeature(0)
    print("GeomTypeByfeature:", feat.geometry().GetGeometryName())
    print("spatialRef:", lyr.GetSpatialRef())  #空间坐标
    print("fieldAttri:",
          [(field.name, field.GetTypeName()) for field in lyr.schema])
    lyrName = lyrName + ".shp"
    pb.print_attributes(os.path.join(fn, lyrName), 3, ["NAME", "KIND"])
Exemplo n.º 2
0
print('First loop')
for feat in lyr:
    print(feat.GetField('Name'), feat.GetField('Population'))
print('Second loop')
lyr.ResetReading()
for feat in lyr:
    pt = feat.geometry()
    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)
Exemplo n.º 3
0
import os
import sys

from osgeo import ogr

import ospybook as pb

# Make sure you change these paths!
original_fn = r'D:\osgeopy-data\Washington\large_cities2.shp'
new_fn = r'D:\osgeopy-data\output\large_cities2.shp'

# Work with a copy instead of the original.
pb.copy_datasource(original_fn, new_fn)

# Print attributes before editing.
pb.print_attributes(new_fn, geom=False)

# Pass a 1 to open for writing.
ds = ogr.Open(new_fn, 1)
if ds is None:
    sys.exit('Could not open {0}.'.format(new_fn))
lyr = ds.GetLayer(0)

# Change a field name.
i = lyr.GetLayerDefn().GetFieldIndex('Name')
fld_defn = ogr.FieldDefn('City_Name', ogr.OFTString)
lyr.AlterFieldDefn(i, fld_defn, ogr.ALTER_NAME_FLAG)

# Add a new field.
lyr.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger))
Exemplo n.º 4
0
lyr = ds.GetLayer(0)
print('First loop')
for feat in lyr:
    print(feat.GetField('Name'), feat.GetField('Population'))
print('Second loop')
lyr.ResetReading() # This is the important line.
for feat in lyr:
    pt = feat.geometry()
    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'])

# 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)
Exemplo n.º 5
0
driver = ogr.GetDriverByName('CSV')
ds = driver.Open(os.path.join(data_dir, 'US'))
print(ds)

#########################  4.2.4 Esri file geodatabase  #######################

# Print out layers in an Esri file geodatabase.
fn = os.path.join(data_dir, 'global', 'natural_earth.gdb')
pb.print_layers(fn)

# Get a layer inside a feature dataset.
ds = ogr.Open(fn)
lyr = ds.GetLayer('countries_10m')

# Print out some attributes to make sure it worked.
pb.print_attributes(lyr, 5, ['NAME', 'POP_EST'])

# Export a feature class from geodatabase to a shapefile.
out_folder = os.path.join(data_dir, 'global')
gdb_ds = ogr.Open(fn)
gdb_lyr = gdb_ds.GetLayerByName('countries_110m')
shp_ds = ogr.Open(out_folder, 1)
shp_ds.CopyLayer(gdb_lyr, 'countries_110m')
del shp_ds, gdb_ds

# Use listing 4.2 to copy shapefiles in a folder into a file geodatabase.
import listing4_2
shp_folder = os.path.join(data_dir, 'global')
gdb_fn = os.path.join(shp_folder, 'osgeopy-data.gdb')
listing4_2.layers_to_feature_dataset(shp_folder, gdb_fn, 'global')
Exemplo n.º 6
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()
Exemplo n.º 7
0
ds = driver.Open(os.path.join(data_dir, 'US'))
print(ds)


#########################  4.2.4 Esri file geodatabase  #######################

# Print out layers in an Esri file geodatabase.
fn = os.path.join(data_dir, 'global', 'natural_earth.gdb')
pb.print_layers(fn)

# Get a layer inside a feature dataset.
ds = ogr.Open(fn)
lyr = ds.GetLayer('countries_10m')

# Print out some attributes to make sure it worked.
pb.print_attributes(lyr, 5, ['NAME', 'POP_EST'])

# Export a feature class from geodatabase to a shapefile.
out_folder = os.path.join(data_dir, 'global')
gdb_ds = ogr.Open(fn)
gdb_lyr = gdb_ds.GetLayerByName('countries_110m')
shp_ds = ogr.Open(out_folder, 1)
shp_ds.CopyLayer(gdb_lyr, 'countries_110m')
del shp_ds, gdb_ds


#########################  4.2.5 Web Feature Service  #########################

# Print out layers in a WFS.
url = 'WFS:http://gis.srh.noaa.gov/arcgis/services/watchwarn/MapServer/WFSServer'
pb.print_layers(url)
Exemplo n.º 8
0
# 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
# in blue. The old filter is no longer in effect.
Exemplo n.º 9
0
print('First loop')
for feat in lyr:
    print(feat.GetField('Name'), feat.GetField('Population'))
print('Second loop')
lyr.ResetReading() # This is the important line.
for feat in lyr:
    pt = feat.geometry()
    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'])

# 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)
Exemplo n.º 10
0
# 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
# in blue. The old filter is no longer in effect.
Exemplo n.º 11
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()