Skip to content

Commit

Permalink
Merge 421127e into 4b00c73
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewphilipsmith committed Aug 28, 2019
2 parents 4b00c73 + 421127e commit 0fa56a4
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@

# Visual Studio Code settings
/.vscode
.coverage
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: python
python:
- '2.7'
install:
- pip install python-coveralls
- pip install coverage
- pip install flake8
- pip install .
before_script:
- flake8 --max-line-length=120
script:
- coverage run --source ./mapactionpy_arcmap -m unittest discover -v ./mapactionpy_arcmap/tests
after_success:
- coveralls
deploy:
provider: pypi
user: asmith_mapaction
password:
secure: w+xquPzLTNX7aeFycj0m1znQJM6f3u55k67rF4kROBXPUnO0zv5iT7HgWIEpOMvGBXzs/TXmXApH4IG0vBQXP2vUTWmQVUswvqV0ojA0yl6NqPrTeHICBF0sk0Cbg8R07hZLbh6hjDo29EwikqsnEhMErBo4/PsRqN0hSxvY7CUwzPmBfibzpJ28NJ2QnbtLJEDvuiO8jayTS9/3JPKeHojgiZ4w+VyDOZ0Kn5Y1c6Z/hitxRVBo8tQ1CErJOwZlWQ4HaDaOazd2lEaH1BxCxqwgDFAco0Z2TrOcZXFDxjL9y+8MkA0wzfQirkzTiMz2NnuzmbRHzVIC6+4Is+iJwdolTK/u1/sJKKdofAJr/8t2eOHxtJOv66oa6QCTMFfcAEizJNHRw3x0k07XT9AGlRh7+xMovq7Tco5rHe8maeaBZPQhp0wkl9vzutLg07DpwuXFOh3PEfpvWig/KZT8n/QOe8KJi1WSrlsNjXzdEcjl01typ9HDUR4OhuRoHiwS1d9Ri3PUNCCGDk4oBehi/PTFDDbAMD1eusM6S9srwBv/KJMMeidfKYORpbq5iLX6hIUokOgQ5iTNN2nHAQjJoYVazj4MVQ9zxZFxSsGDHn+Gnmft67ok0L7DtgH1gcT0FnpLwuNPXBHAD90V7osb4KkttlKLzhWazufmOy2KP38=
skip_existing: true
on:
all_branches: true
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

# MapChef

Master branch [![Build Status](https://travis-ci.org/mapaction/mapactionpy_arcmap.svg?branch=master)](https://travis-ci.org/mapaction/mapactionpy_arcmap) [![Coverage Status](https://coveralls.io/repos/github/mapaction/mapactionpy_arcmap/badge.svg?branch=pep8-and-travis)](https://coveralls.io/github/mapaction/mapactionpy_arcmap?branch=master)

Adds geospatial data to an ArcMap MXD file based on a recipe from a cookbook configuration file and a product name.

### Prerequisites
Expand Down
14 changes: 7 additions & 7 deletions mapactionpy_arcmap/LayerProperties.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import json
import os
import json
from LayerProperty import LayerProperty


class LayerProperties:
def __init__(self, layerPropertiesJsonFile):
self.layerPropertiesJsonFile=layerPropertiesJsonFile
self.properties = set();
self.layerPropertiesJsonFile = layerPropertiesJsonFile
self.properties = set()

def parse(self):
with open(self.layerPropertiesJsonFile) as json_file:
with open(self.layerPropertiesJsonFile) as json_file:
jsonContents = json.load(json_file)
for layer in jsonContents['layerProperties']:
property = LayerProperty(layer)
#print (property.layerName)
# print (property.layerName)
self.properties.add(property)

def get(self, layerName):
for property in self.properties:
if (property.layerName == layerName):
# Find shape file if it exists
return property
return None
return None
14 changes: 7 additions & 7 deletions mapactionpy_arcmap/LayerProperty.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class LayerProperty:
def __init__(self, row):
self.mapFrame = row["MapFrame"]
self.layerGroup = row["LayerGroup"]
self.layerName = row["LayerName"]
self.sourceFolder = row["SourceFolder"]
self.regExp = row["RegExp"]
self.definitionQuery = row["DefinitionQuery"]
self.display = row["Display"]
self.mapFrame = row["MapFrame"]
self.layerGroup = row["LayerGroup"]
self.layerName = row["LayerName"]
self.sourceFolder = row["SourceFolder"]
self.regExp = row["RegExp"]
self.definitionQuery = row["DefinitionQuery"]
self.display = row["Display"]
59 changes: 31 additions & 28 deletions mapactionpy_arcmap/MapChef.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from os import listdir
from os.path import isfile, join
import arcpy
from arcpy import env
import re
from MapRecipe import MapRecipe
from MapCookbook import MapCookbook
from MapReport import MapReport
from MapResult import MapResult
from LayerProperties import LayerProperties


class MapChef:
def __init__(self, mxd, cookbookJsonFile, layerPropertiesJsonFile, crashMoveFolder, layerDirectory):
self.mxd = mxd
Expand All @@ -27,7 +26,7 @@ def readCookbook(self):
return cookbook

def readLayerPropertiesFile(self):
layerProperties = LayerProperties(self.layerPropertiesJsonFile)
layerProperties = LayerProperties(self.layerPropertiesJsonFile)
layerProperties.parse()
return layerProperties

Expand Down Expand Up @@ -56,25 +55,28 @@ def cook(self, productName, countryName):
properties = self.layerProperties.get(layer)
# Add layer to the report for later
mapResult = MapResult(layer)
if (properties is not None):
if (properties is not None):
layerFilePath = os.path.join(self.layerDirectory, (properties.layerName + ".lyr"))
if (os.path.exists(layerFilePath)):
self.dataFrame = arcpy.mapping.ListDataFrames(self.mxd, properties.mapFrame)[0]
layerToAdd = arcpy.mapping.Layer(layerFilePath)
dataFilePath= os.path.join(self.root, "GIS", "2_Active_Data", properties.sourceFolder)
dataFilePath = os.path.join(self.root, "GIS", "2_Active_Data", properties.sourceFolder)
if (os.path.isdir(dataFilePath)):
if ("/" not in properties.regExp):
onlyfiles = [f for f in listdir(dataFilePath) if isfile(join(dataFilePath, f))]
for fileName in onlyfiles:
if re.match(properties.regExp, fileName):
self.dataFrame = arcpy.mapping.ListDataFrames(self.mxd, properties.mapFrame)[0]
dataFile = os.path.join(dataFilePath, fileName)
mapResult.added = self.addToLayer(self.dataFrame, dataFile, layerToAdd, properties.definitionQuery, properties.display, countryName)
mapResult.added = self.addToLayer(
self.dataFrame, dataFile, layerToAdd, properties.definitionQuery,
properties.display, countryName)
mapResult.dataSource = dataFile
if (mapResult.added == True):
if mapResult.added:
mapResult.message = "Layer added successfully"
else:
mapResult.message = "Unexpected schema. Could not evaluate expression: " + properties.definitionQuery
mapResult.message = ("Unexpected schema. Could not evaluate expression: "
+ properties.definitionQuery)
break
else:
# It's a File Geodatabase
Expand All @@ -86,24 +88,25 @@ def cook(self, productName, countryName):
arcpy.env.workspace = rasterFile
rasters = arcpy.ListRasters("*")
for raster in rasters:
if re.match(parts[1], raster):
rasterLayer = (rasterFile + "\\" + raster)
self.dataFrame = arcpy.mapping.ListDataFrames(self.mxd, properties.mapFrame)[0]
self.addRasterToLayer(self.dataFrame, rasterFile, layerToAdd, raster, properties.display)
mapResult.dataSource = rasterFile
mapResult.added = True
if re.match(parts[1], raster):
self.dataFrame = arcpy.mapping.ListDataFrames(
self.mxd, properties.mapFrame)[0]
self.addRasterToLayer(self.dataFrame, rasterFile,
layerToAdd, raster, properties.display)
mapResult.dataSource = rasterFile
mapResult.added = True
# If a file hasn't been added, and no other reason given, report what was expected
if ((mapResult.added == False) and (len(mapResult.message) == 0)):
mapResult.message = "Could not find file matching " + properties.sourceFolder + "/" + properties.regExp
if ((mapResult.added is False) and (len(mapResult.message) == 0)):
mapResult.message = ("Could not find file matching "
+ properties.sourceFolder + "/" + properties.regExp)
else:
mapResult.added = False
mapResult.message = "Layer file could not be found"
mapResult.added = False
mapResult.message = "Layer file could not be found"
else:
mapResult.added = False
mapResult.message = "Layer property definition could not be found in the cookbook"
self.mapReport.add(mapResult)


# Make all layers visible
self.enableLayers()

Expand All @@ -113,34 +116,34 @@ def cook(self, productName, countryName):
self.mxd.save()

def addRasterToLayer(self, dataFrame, rasterFile, layer, raster, display):
for lyr in arcpy.mapping.ListLayers(layer):
for lyr in arcpy.mapping.ListLayers(layer):
lyr.replaceDataSource(rasterFile, "FILEGDB_WORKSPACE", raster)
lyr.visible = False
arcpy.mapping.AddLayer(dataFrame, lyr, "BOTTOM")
lyr.visible = False
arcpy.mapping.AddLayer(dataFrame, lyr, "BOTTOM")

def addToLayer(self, dataFrame, dataFile, layer, definitionQuery, display, countryName):
added = True
dataDirectory = os.path.dirname(os.path.realpath(dataFile))
for lyr in arcpy.mapping.ListLayers(layer):
# https://community.esri.com/thread/60097
base=os.path.basename(dataFile)
base = os.path.basename(dataFile)
extension = os.path.splitext(base)[1]
if (extension.upper() == ".SHP"):
lyr.replaceDataSource(dataDirectory, "SHAPEFILE_WORKSPACE", os.path.splitext(base)[0])
lyr.replaceDataSource(dataDirectory, "SHAPEFILE_WORKSPACE", os.path.splitext(base)[0])
if ((extension.upper() == ".TIF") or (extension.upper() == ".IMG")):
lyr.replaceDataSource(dataDirectory, "RASTER_WORKSPACE", os.path.splitext(base)[0])
lyr.replaceDataSource(dataDirectory, "RASTER_WORKSPACE", os.path.splitext(base)[0])
if (definitionQuery):
definitionQuery = definitionQuery.replace('{COUNTRY_NAME}', countryName)
# https://gis.stackexchange.com/questions/90736/setting-definition-query-on-arcpy-layer-from-shapefile
# https://gis.stackexchange.com/questions/90736/setting-definition-query-on-arcpy-layer-from-shapefile
lyr.definitionQuery = definitionQuery
try:
arcpy.SelectLayerByAttribute_management(lyr, "SUBSET_SELECTION", definitionQuery)
except Exception as e:
except Exception:
added = False
lyr.visible = False
if (added):
arcpy.mapping.AddLayer(dataFrame, lyr, "BOTTOM")
return added

def report(self):
return self.mapReport.dump()
return self.mapReport.dump()
7 changes: 4 additions & 3 deletions mapactionpy_arcmap/MapCookbook.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from MapRecipe import MapRecipe
import json
import json


class MapCookbook:
def __init__(self, cookbookJsonFile):
self.cookbookJsonFile=cookbookJsonFile
self.cookbookJsonFile = cookbookJsonFile
self.products = list()

def parse(self):
with open(self.cookbookJsonFile) as json_file:
with open(self.cookbookJsonFile) as json_file:
jsonContents = json.load(json_file)
for recipe in jsonContents['recipes']:
rec = MapRecipe(recipe['product'], recipe['layers'])
Expand Down
2 changes: 1 addition & 1 deletion mapactionpy_arcmap/MapRecipe.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MapRecipe:
def __init__(self, product, layers):
self.product = product
self.layers = layers;
self.layers = layers
17 changes: 9 additions & 8 deletions mapactionpy_arcmap/MapReport.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,29 @@ def dump(self):
for report in self.results:
if (reportIter > 0):
resultJson = resultJson + ","
resultJson = resultJson + report.toJSON()
if (report.added == False):
resultJson = resultJson + report.toJSON()
if not report.added:
failCount = failCount + 1
reportIter = reportIter + 1

resultJson = resultJson + "]}"

if (reportIter == 0):
self.result = "Failure";
self.result = "Failure"
self.summary = "No layers provided in recipe for '" + self.productName + "' product."
else:
if (failCount == 0):
self.result = "Success"
self.summary = "'" + self.productName + "' product generated successfully."
else:
self.result = "Warning"
self.summary = str(failCount) + " / " + str(reportIter) + " layers could not be added to '" + self.productName + "' product."
self.summary = str(failCount) + " / " + str(reportIter) + \
" layers could not be added to '" + self.productName + "' product."

resultJson = "{\"result\":\"" + self.result + \
"\", \"productName\":\"" + self.productName + \
"\", \"summary\":\"" + self.summary + \
"\", \"classification\":\"" + self.classification + \
"\", \"results\":[" + resultJson
"\", \"productName\":\"" + self.productName + \
"\", \"summary\":\"" + self.summary + \
"\", \"classification\":\"" + self.classification + \
"\", \"results\":[" + resultJson

return resultJson
5 changes: 3 additions & 2 deletions mapactionpy_arcmap/MapResult.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from datetime import datetime


class MapResult:
def __init__(self, layerName):
self.layerName = layerName
Expand All @@ -16,7 +17,7 @@ def toJSON(self):
"layerName": self.layerName,
"dataSource": self.dataSource,
"dateStamp": self.timeStamp,
"added" : self.added,
"added": self.added,
"message": self.message
}
return (json.dumps(result))
return (json.dumps(result))
14 changes: 7 additions & 7 deletions mapactionpy_arcmap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .LayerProperty import LayerProperty
from .LayerProperties import LayerProperties
from .MapRecipe import MapRecipe
from .MapChef import MapChef
from .MapCookbook import MapCookbook
from .MapReport import MapReport
from .MapResult import MapResult
# from .LayerProperty import LayerProperty
# from .LayerProperties import LayerProperties
# from .MapRecipe import MapRecipe
# from .MapChef import MapChef
# from .MapCookbook import MapCookbook
# from .MapReport import MapReport
# from .MapResult import MapResult

0 comments on commit 0fa56a4

Please sign in to comment.