Skip to content
Permalink
Browse files

Rewrite existing examples to be py3 compatible

  • Loading branch information...
geographika committed Mar 27, 2019
1 parent c5cfd1d commit 1bb141453973337389cdfffbc88a0638d0da3714
Showing with 199 additions and 129 deletions.
  1. +92 −62 mapscript/python/examples/shpdump.py
  2. +107 −67 mapscript/python/examples/shpinfo.py
@@ -1,62 +1,92 @@
#!/usr/bin/env python

""" Dump the contents of the passed in shapefile.
Pass in the basename of the shapefile. This script uses the mapscript
extention module.
"""

import mapscript
import sys
import os

# Utility functions.
def usage():
""" Display usage if program is used incorrectly. """
print "Syntax: %s base_filename" % sys.argv[0]
sys.exit(2)

def plural(x):
""" Returns an 's' if plural.
Useful in print statements to avoid something like 'point(s)'. """
if x > 1:
return 's'
return ''


# Make sure passing in filename argument.
if len(sys.argv) != 2:
usage()

# Make sure can access .shp file, create shapefileObj.
if os.access(sys.argv[1] + ".shp", os.F_OK):
sf_obj = mapscript.shapefileObj(sys.argv[1], -1)
else:
print "Can't access shapefile"
sys.exit(2)

# Create blank shape object.
s_obj = mapscript.shapeObj(-1)

# Loop through each shape in the shapefile.
for i in range(sf_obj.numshapes):

# Get the ith object.
sf_obj.get(i, s_obj)
print "Shape %i has %i part%s." % (i, s_obj.numlines, plural(s_obj.numlines))
print "bounds (%f, %f) (%f, %f)" % (s_obj.bounds.minx, s_obj.bounds.miny, s_obj.bounds.maxx, s_obj.bounds.maxy)

# Loop through parts of each shape.
for j in range(s_obj.numlines):

# Get the jth part of the ith object.
part = s_obj.get(j)
print "Part %i has %i point%s." % (j, part.numpoints, plural(part.numpoints))

# Loop through points in each part.
for k in range(part.numpoints):

# Get the kth point of the jth part of the ith shape.
point = part.get(k)
print "%i: %f, %f" % (k, point.x, point.y)
"""
Dump the contents of the passed in Shapefile
Usage:
python shpdump.py polygon.shp
"""

import mapscript
import sys
import os


def plural(x):
"""
Returns an 's' if plural
Useful in print statements to avoid something like 'point(s)'
"""
if x > 1:
return 's'
return ''


def get_shapefile_object(sf_path):

# make sure can access .shp file, create shapefileObj

if os.access(sf_path, os.F_OK):
sf_obj = mapscript.shapefileObj(sf_path, -1)
else:
print("Can't access {}".format(sf_path))
sys.exit(2)

return sf_obj


def main(sf_path):

if not sf_path.lower().endswith(".shp"):
sf_path += ".shp"

sf_obj = get_shapefile_object(sf_path)

# create an empty Shapefile object

s_obj = mapscript.shapeObj()

# loop through each shape in the original Shapefile

for i in range(sf_obj.numshapes):

# get the object at index i
sf_obj.get(i, s_obj)
print("Shape %i has %i part%s" % (i, s_obj.numlines, plural(s_obj.numlines)))
print("Bounds (%f, %f) (%f, %f)" % (s_obj.bounds.minx, s_obj.bounds.miny, s_obj.bounds.maxx, s_obj.bounds.maxy))

# loop through parts of each shape

for j in range(s_obj.numlines):

# get the jth part of the ith object

part = s_obj.get(j)
print("Part %i has %i point%s" % (j, part.numpoints, plural(part.numpoints)))

# loop through points in each part

for k in range(part.numpoints):

# get the kth point of the jth part of the ith shape

point = part.get(k)
print("%i: %f, %f" % (k, point.x, point.y))


def usage():
"""
Display usage if program is used incorrectly
"""
print("Syntax: %s <shapefile_path>" % sys.argv[0])
sys.exit(2)


# make sure passing in filename argument
if len(sys.argv) != 2:
usage()


sf_path = sys.argv[1]
main(sf_path)
@@ -1,67 +1,107 @@
#!/usr/bin/env python

""" Extracts basic descriptive information from the shapefile.
"""

import mapscript
import sys
import os

# Utility functions.
def usage():
""" Display usage if program is used incorrectly. """
print "Syntax: %s base_filename" % sys.argv[0]
sys.exit(2)

# Make sure passing in filename argument.
if len(sys.argv) != 2:
usage()

# Make sure can access .shp file, create shapefileObj.
if os.access(sys.argv[1] + ".shp", os.F_OK):
sf_obj = mapscript.shapefileObj(sys.argv[1], -1)
else:
print "Can't access shapefile"
sys.exit(2)

# Dictionary of shapefile types.
types = { 1: 'point',
3: 'arc',
5: 'polygon',
8: 'multipoint' }

# Print out basic information that is part of the shapefile object.
print "Shapefile %s:" % sys.argv[1]
print
print "\ttype: %s" % types[sf_obj.type]
print "\tnumber of features: %i" % sf_obj.numshapes
print "\tbounds: (%f, %f) (%f, %f)" % (sf_obj.bounds.minx,
sf_obj.bounds.miny,
sf_obj.bounds.maxx,
sf_obj.bounds.maxy)

# Including a class to read DBF files.
# If not found, quit because can't do anything else without the dbfreader class.
try:
import dbfreader
except:
# Can't do anymore.
sys.exit(2)

# Create DBF object.
dbf_obj = dbfreader.DBFFile(sys.argv[1] + ".dbf")

# Print out table characteristics.
print ""
print "\tDBF file: %s" % sys.argv[1] + ".dbf"
print "\tnumber of records: %i" % dbf_obj.get_record_count()
print "\tnumber of fields: %i" % len(dbf_obj.get_fields())
print ""
print "\t%-20s %12s %8s %10s" % ("Name", "Type", "Length", "Decimals")
print "\t-----------------------------------------------------"

# Print out field characteristics.
for field in dbf_obj.get_fields():
print "\t%-20s %12s %8d %10d" % (field.get_name(), field.get_type_name(), field.get_len(), field.field_places)

"""
Extracts basic descriptive information from a Shapefile
Usage:
python shpinfo.py point.shp
"""
import mapscript
import sys
import os


def get_shapefile_object(sf_path):

# make sure can access .shp file, create shapefileObj

if os.access(sf_path, os.F_OK):
sf_obj = mapscript.shapefileObj(sf_path)
else:
print("Can't access {}".format(sf_path))
sys.exit(2)

return sf_obj


def get_shapefile_details(sf_obj):

# dictionary of shapefile types

types = {
mapscript.MS_SHAPEFILE_POINT: 'Point',
mapscript.MS_SHAPEFILE_ARC: 'Line',
mapscript.MS_SHAPEFILE_POLYGON: 'Polygon',
mapscript.MS_SHAPEFILE_MULTIPOINT: 'Multipoint',

mapscript.MS_SHP_POINTZ: 'PointZ',
mapscript.MS_SHP_ARCZ: 'LineZ',
mapscript.MS_SHP_POLYGONZ: 'PolygonZ',
mapscript.MS_SHP_MULTIPOINTZ: 'MultipointZ',

mapscript.MS_SHP_POINTM: 'Multipoint',
mapscript.MS_SHP_ARCM: 'LineM',
mapscript.MS_SHP_POLYGONM: 'PolygonM',
mapscript.MS_SHP_MULTIPOINTM: 'MultipointM'
}

# print out basic information that is part of the shapefile object

print("\tType: %s" % types[sf_obj.type])

print("\tBounds: (%f, %f) (%f, %f)" % (sf_obj.bounds.minx,
sf_obj.bounds.miny,
sf_obj.bounds.maxx,
sf_obj.bounds.maxy))

print("\tNumber of features: %i" % sf_obj.numshapes)


def get_dbf_details(sf_obj):

# get DBF object

dbf_obj = sf_obj.getDBF()

# print out table characteristics

print("\tNumber of records in DBF: %i" % dbf_obj.nRecords)
print("\tNumber of fields: %i" % dbf_obj.nFields)
print("")
print("\t%-20s %12s %8s %10s" % ("Name", "Type", "Length", "Decimals"))
print("\t-----------------------------------------------------")

# print out field characteristics

for idx in range(0, dbf_obj.nFields):
print("\t%-20s %12s %8d %10d" % (dbf_obj.getFieldName(idx), dbf_obj.getFieldType(idx),
dbf_obj.getFieldWidth(idx), dbf_obj.getFieldDecimals(idx)))


def main(sf_path):

if not sf_path.lower().endswith(".shp"):
sf_path += ".shp"

sf_obj = get_shapefile_object(sf_path)

print("Shapefile %s:" % sf_path)
print("")

get_shapefile_details(sf_obj)
get_dbf_details(sf_obj)


def usage():
"""
Display usage if program is used incorrectly
"""
print("Syntax: %s <shapefile_path>" % sys.argv[0])
sys.exit(2)


# make sure a filename argument is provided
if len(sys.argv) != 2:
usage()

main(sys.argv[1])

0 comments on commit 1bb1414

Please sign in to comment.
You can’t perform that action at this time.