Select to view content in your preferred language

XSLT Transformation  / Thumbnails

2548
2
06-18-2013 09:17 PM
CPoynter
Occasional Contributor III
Hi All,

I have two issues. I am running ArcGIS 10.1 Windows 7 64 bit (those are not the issues - hopefully).

1) I am trying to use the XSLT Transformation Conversion but outside of ArcGIS Python window it does not run. IDLE / PyScripter just hang at this section of code. Same code runs fine within the python window of ArcGIS 10.1;

2) I have a number of thumbnails that I have created that I would like to utilise in the the created HTML pages produced by a functioning XSLT Transformation Conversion, but don't know how to get them into the output HTML.

I believe the thumbnails need to be incorporated at some point into metadata files for my datasets, so I guess I am missing a section of code relating to editing metadata.

Following is my code:


##
## 1) Create a blank MXD
## 2) Loop through data and add to MXD to export Thumbnails
## 3) Create HTML metadata pages incorporating Thumbnails
##

import arcpy, os, json, traceback, sys, datetime
from arcpy import mapping
from arcpy import env

mxd_out = r'D:\Temp\HTML.mxd'

now = datetime.datetime.now()
log = r'D:\Temp\CSD_HTML\Invertory_' + now.strftime("%Y-%m-%d" + '_' + "%H_%M_%S") + '_Error.log'

if arcpy.Exists(log):
                          arcpy.Delete_management(log)

if arcpy.Exists(mxd_out):
                          arcpy.Delete_management(mxd_out)

mapService = {}
jsonDump = json.dumps(mapService)
result = mapping.ConvertWebMapToMapDocument(jsonDump)
mxd = result.mapDocument
my_mxd = mxd.saveACopy(mxd_out)

env.workspace = r"D:\Temp\Data"

contents = []

print 'Starting'

for dirpath, dirnames, datatypes in arcpy.da.Walk(env.workspace):
        contents.append(os.path.join(dirpath))

        for item in contents:
            if arcpy.Exists(item):

                arcpy.env.workspace = item

        listType = [arcpy.ListFeatureClasses, arcpy.ListDatasets]
        for list in listType:

            datasetList = list("*", 'All')

            # Iterate over the feature classes
            for dataset in datasetList:

                    data = item + '\\' + dataset

                    desc = arcpy.Describe(data)

                    print data + ' : ' + desc.dataType

                    # Crack open the map
                    mxd_new = arcpy.mapping.MapDocument(mxd_out)

                    df = arcpy.mapping.ListDataFrames(mxd_new, '*')[0]

                    try:

                            lyrFile = arcpy.mapping.Layer(data)

                            if desc.dataType == 'RasterDataset':
                                layerType = arcpy.management.MakeRasterLayer

                                result = layerType(lyrFile, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)

                            else:
                                result = arcpy.management.MakeFeatureLayer(lyrFile, 'temp_layer')
                                layer_object = result.getOutput(0)
                                arcpy.mapping.AddLayer(df, layer_object)

                            # Set correct extent
                            df.zoomToSelectedFeatures()

                            nm = os.path.splitext(dataset)[0]

                            # Compute an output file name
                            out_file_name = (r"D:\Temp\Thumbs" + "\\" + nm + '.jpg')

                            # Export "thumbnail" of data frame
                            arcpy.mapping.ExportToJPEG(mxd_new, out_file_name, df, 300, 300)

                            # Pull the layer out of the data frame to make room for the next one
                            arcpy.mapping.RemoveLayer(df, layer_object)

                            # Delete the GP layer
                            arcpy.management.Delete('temp_layer')


                            #set local variables
                            dir = arcpy.GetInstallInfo("desktop")["InstallDir"]
                            xslt = dir + "Metadata/Stylesheets/ArcGIS.xsl"
                            arcpy.XSLTransform_conversion(data, xslt, r'D:\Temp' + '\\' + nm + '.html', out_file_name)

##                            xslt = r"C:\Program Files (x86)\ArcGIS\Desktop10.1\Metadata\Stylesheets\ArcGIS.xsl"
##                            arcpy.XSLTransform_conversion(data, xslt, r'D:\Temp' + '\\' + nm + '.html', out_file_name)

                    except:

                        arcpy.ExecuteError
                        arcpy.AddError(arcpy.GetMessage(2))
                        f=open(log, 'at')
                        f.write(data + '\n')
                        f.close()

del mxd, mxd_new

if arcpy.Exists(mxd_out):
                          arcpy.Delete_management(mxd_out)

del mxd_out



Guidance greatly appreciated.

Regards,

Craig
Tags (2)
0 Kudos
2 Replies
SteveRichards
New Contributor
Hi,

I have the opposite 'problem' I was trying to figure out how to get the thumbnails and came across your post.
What I am planning on doing is to use python to rewrite the html and substitute:
<div class="noThumbnail">Thumbnail Not Available</div>
with
<p align = "center"><img src = "./Thumbs/layer.jpg" /></p>

I'll update when I complete the solution

Steve
0 Kudos
CPoynter
Occasional Contributor III
Hi Steve,

I have pulled the relevant code for you that I eventually sorted out. You might need to play with it a little to get what you require.


fh = open(r'D:\Temp\CSD_HTML\CSD_QandA.html', 'wb')

html_head = """

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>"""

fh.write(html_head)

 html_body = """
<body>
<table width="799" height="317" border="0" cellspacing="1">
                                                                              
<tr>
   <td width="309" rowspan="5"><img src="Thumbs/""" + nm + """.jpg" alt="" width="300" height="300" align="left"></td>
   <td width="4" rowspan="5"> </td>
   <td height="50" colspan="3">Title: """ + title + """</td>
</tr>
                                                                              
</table>
</body>"""
                                                                              
fh.write(html_body)
                                                                           
html_tail = """
</html>"""

fh.write(html_tail)
fh.close()
del fh



I broke it up into 3 sections, head, body and tail. The body section is where you can play around with adding thumbnail and other information. I designed my webpage first and then copied and modified the HTML code for use within my script.

Regards,

Craig
0 Kudos