Skip to content

Commit

Permalink
Minor fixes after testing with some "noisy" data
Browse files Browse the repository at this point in the history
Looks like a lot of changes, but really just a few:
1. Added some checks that input image files exist
2. Added an exception handler to the row process
3. Added the ability to use tag other than "vector" for these (I needed
to use tag "svg")
4. Added a few switches to turn off a few things I personally didn't
need (but kept the defaults as they were originally)
  • Loading branch information
csmoore committed Apr 21, 2014
1 parent df47e4e commit 7756a5f
Showing 1 changed file with 124 additions and 29 deletions.
153 changes: 124 additions & 29 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Text;
Expand Down Expand Up @@ -27,7 +41,7 @@ static void Main(string[] args)
{
Console.WriteLine("");
Console.WriteLine("Invalid parameters specified");
Console.WriteLine("Usage: csv2ArcGISStyle <csvPath> <stylePath>");
Console.WriteLine("Usage: csv2ArcGISStyle <csvFullPath> <styleFullPath>");
Console.WriteLine("");
Console.WriteLine("Example:");
Console.WriteLine("csv2ArcGISStyle C:\\style\\myStyleSample.csv c:\\style\\myStyleSample.style");
Expand Down Expand Up @@ -58,11 +72,15 @@ static void Main(string[] args)
ImportCSV(csvPath, stylePath);
ConvertVectorPicturesToRepresentationMarkers(stylePath);

//do the Server style
Console.WriteLine("");
Console.WriteLine("Creating the server style:");
ImportCSV(csvPath, serverStylePath);
ConvertVectorPicturesToRepresentationMarkers(serverStylePath);
// do the Server styles + switch to disable if desired
const bool DO_SERVER_STYLES = true;
if (DO_SERVER_STYLES)
{
Console.WriteLine("");
Console.WriteLine("Creating the server style:");
ImportCSV(csvPath, serverStylePath);
ConvertVectorPicturesToRepresentationMarkers(serverStylePath);
}

Console.WriteLine("");
Console.WriteLine("Import complete, new styles " + stylePath + " and " + serverStylePath + " created");
Expand All @@ -72,6 +90,7 @@ static void Main(string[] args)
m_AOLicenseInitializer.ShutdownApplication();

}

static void ImportCSV(string csvPath, string stylePath)
{
//the expected field names for this util
Expand All @@ -86,7 +105,6 @@ static void ImportCSV(string csvPath, string stylePath)
IStyleGalleryStorage styleGalleryStorage = styleGallery as IStyleGalleryStorage;
styleGalleryStorage.TargetFile = stylePath;


ITable table = OpenCSVAsTable(csvPath);
IRow row = null;
int filePathIdx, styleItemNameIdx, pointSizeIdx, styleItemCategoryIdx, styleItemTagsIdx;
Expand All @@ -103,33 +121,76 @@ static void ImportCSV(string csvPath, string stylePath)
styleItemCategoryIdx = cursor.FindField(styleItemCategory);
styleItemTagsIdx = cursor.FindField(styleItemTags);

int rowCount = 0;
while ((row = cursor.NextRow()) != null)
{
String itemFilePath = (string)row.get_Value(filePathIdx);
IPictureMarkerSymbol pictureMarkerSymbol = MakeMarkerSymbol(itemFilePath, Convert.ToDouble(row.get_Value(pointSizeIdx)));
styleGalleryItem = new StyleGalleryItemClass();
styleGalleryItem.Item = pictureMarkerSymbol;
styleGalleryItem.Name = row.get_Value(styleItemNameIdx) as string;
styleGalleryItem.Category = row.get_Value(styleItemNameIdx) as string;
styleGalleryItem.Tags = row.get_Value(styleItemTagsIdx) as string;

//we want tags for search. If they weren't specified, use the default tags
if (styleGalleryItem.Tags == "")
rowCount++;

try
{
styleGalleryItem.Tags = styleGalleryItem.DefaultTags;
String itemFilePath = (string)row.get_Value(filePathIdx);
IPictureMarkerSymbol pictureMarkerSymbol = MakeMarkerSymbol(itemFilePath, Convert.ToDouble(row.get_Value(pointSizeIdx)));

if (pictureMarkerSymbol == null)
{
Console.WriteLine("Image not found: Row: " + rowCount + ", Image: " + itemFilePath);
continue;
}

styleGalleryItem = new StyleGalleryItemClass();
styleGalleryItem.Item = pictureMarkerSymbol;
styleGalleryItem.Name = row.get_Value(styleItemNameIdx) as string;
styleGalleryItem.Category = row.get_Value(styleItemCategoryIdx) as string;
object obj = row.get_Value(styleItemTagsIdx);

// set a default tag, just in case tags field is empty
string tags = styleGalleryItem.Category + ";" + styleGalleryItem.Name;

// Make sure tags are set & a valid string type before converting to string
if (!((obj == null) || (obj is DBNull)))
tags = obj as string;

const int MAX_TAG_LENGTH = 255;

int tagsLength = tags.Length;
if (tagsLength > MAX_TAG_LENGTH)
{
// WORKAROUND if length > 255 (a hard limit), trim from the front (last most important)
tags = tags.Substring(tagsLength - MAX_TAG_LENGTH, MAX_TAG_LENGTH);
}

// Note: Tag vector symbols for later (see: containsVectorTag)
// + switch to disable if desired
const bool TAG_EMFS_WITH_VECTOR_TAG = true;
if (TAG_EMFS_WITH_VECTOR_TAG &&
(itemFilePath.Substring(itemFilePath.Length - 4) == ".emf") &&
(!tags.ToUpper().Contains("VECTOR")))
{
tags = tags + ";vector";
}

styleGalleryItem.Tags = tags;

//we want tags for search. If they weren't specified, use the default tags
if (styleGalleryItem.Tags == "")
{
styleGalleryItem.Tags = styleGalleryItem.DefaultTags;
}

Console.WriteLine("Importing symbol " + rowCount + " : " + styleGalleryItem.Name);
styleGallery.AddItem((IStyleGalleryItem)styleGalleryItem);
}

if (itemFilePath.Substring(itemFilePath.Length - 4) == ".emf")
catch (Exception ex)
{
styleGalleryItem.Tags = styleGalleryItem.Tags + ";vector";
// Catch-all exception for row processing, just so 1 bad row doesn't abort the whole process
Console.WriteLine("Skipping bad row: " + rowCount + ", Exception : " + ex.Message);
continue;
}

Console.WriteLine("Importing symbol " + styleGalleryItem.Name);
styleGallery.AddItem((IStyleGalleryItem)styleGalleryItem);
}
}

}

private static ITable OpenCSVAsTable(string csvPath)
{
IWorkspaceFactory wsFactory = new TextFileWorkspaceFactoryClass();
Expand All @@ -143,16 +204,47 @@ private static ITable OpenCSVAsTable(string csvPath)

private static IPictureMarkerSymbol MakeMarkerSymbol(string filePath, double size)
{
if (!System.IO.File.Exists(filePath))
{
Console.WriteLine("Source MarkerSymbol does NOT exist: " + filePath);
return null;
}

bool isVector = (filePath.Substring(filePath.Length - 4) == ".emf");
esriIPictureType picType = (isVector) ? esriIPictureType.esriIPictureEMF : esriIPictureType.esriIPicturePNG;

IPictureMarkerSymbol pictureMarkerSymbol = new PictureMarkerSymbolClass();
pictureMarkerSymbol.CreateMarkerSymbolFromFile(picType, filePath);
pictureMarkerSymbol.Size = size;
return pictureMarkerSymbol;
}

// A list of tags that identify a marker as a vector type
private static List<string> vectorTags = new List<string>() { "EMF", "SVG", "VECTOR" };

private static bool containsVectorTag(string tags)
{
string tagsUpper = tags.ToUpper();

foreach (string vectorTag in vectorTags)
{
if (tagsUpper.Contains(vectorTag))
{
return true;
}
}

return false;
}

private static void ConvertVectorPicturesToRepresentationMarkers(string stylePath)
{
if (!System.IO.File.Exists(stylePath))
{
Console.WriteLine("StylePath does NOT exist: " + stylePath);
return;
}

IStyleGallery styleGallery = GetStyleGallery(stylePath);
IStyleGalleryStorage styleGalleryStorage = styleGallery as IStyleGalleryStorage;
styleGalleryStorage.TargetFile = stylePath;
Expand All @@ -168,7 +260,9 @@ private static void ConvertVectorPicturesToRepresentationMarkers(string stylePat
IPictureMarkerSymbol pictureMS = originalItem.Item as IPictureMarkerSymbol;
if (pictureMS != null)
{
if (originalItem.Tags.Contains(";vector"))
// check if it requires conversion to rep marker?
// TODO: perhaps make this an option if user doesn't need rep's
if (containsVectorTag(originalItem.Tags))
{
newItem = ConvertMarkerItemToRep(originalItem);
Console.WriteLine("Converting symbol " + newItem.Name + " to a representation marker");
Expand All @@ -178,8 +272,8 @@ private static void ConvertVectorPicturesToRepresentationMarkers(string stylePat
}
}


}

private static IStyleGalleryItem3 ConvertMarkerItemToRep(IStyleGalleryItem3 inputItem)
{
IMarkerSymbol markerSymbol = inputItem.Item as IMarkerSymbol;
Expand Down Expand Up @@ -220,9 +314,8 @@ private static IStyleGalleryItem3 ConvertMarkerItemToRep(IStyleGalleryItem3 inpu
newMarkerStyleGalleryItem.Tags = inputItem.Tags.Replace(";emf", ""); //strip emf from the tags

return newMarkerStyleGalleryItem;


}

static IStyleGallery GetStyleGallery(string stylePath)
{
IStyleGallery styleGallery = null;
Expand All @@ -237,5 +330,7 @@ static IStyleGallery GetStyleGallery(string stylePath)
}
return styleGallery;
}

}

}

0 comments on commit 7756a5f

Please sign in to comment.