public class MainOps
{
public CGridInfo oGrid;
public DataManagement oDataMan;
public Layers oLayers;
public string sInitWorkingDir = ConfigurationSettings.AppSettings["initialWorkingDir"];
public string sInputDir = ConfigurationSettings.AppSettings["inputDir"];
public string sClutterPtsShpFileName = ConfigurationSettings.AppSettings["clutterPoints"];
public MainOps(CGridInfo objGrid, DataManagement objDataMan, Layers objLayers)
{
oGrid = objGrid;
oDataMan = objDataMan;
oLayers = objLayers;
}
public void InitialProcess()
{
IFeatureClass pRasToPolyFeatClass;
IFeatureClass pMeanPointFeatClass;
IFeatureClass pClutterFeatClass;
Tables2 oTables2 = new Tables2();
OutputInfo winOutputInfo = new OutputInfo();
SpatialOps oSpatialOps = new SpatialOps();
IRaster pGrid;
int[] iaUniqueCellIds;
IFeatureCursor pPolyFeatCursor;
ArrayList alPointFeatures = new ArrayList();
IPoint pWeightedPoint = null;
SpatialOps.Threshold pThesh = new CellCentroid.SpatialOps.Threshold();
IPoint pWGS84point = new PointClass();
string sCellName = null;
string sRNC = null;
string sBSC = null;
try
{
//Copy from network a copy of the latest UMTS raster into working directory
oLayers.CopyRaster(oGrid.CurrentVersionLocation, oGrid.CurrentVersionName, sInitWorkingDir,
oGrid.CurrentVersionName);
winOutputInfo.lblStartTime.Text = DateTime.Now.ToShortTimeString();
winOutputInfo.lblBearerName.Text = oGrid.CurrentVersionName;
winOutputInfo.Show();
winOutputInfo.Refresh();
pGrid = oLayers.OpenRaster(sInitWorkingDir, oGrid.CurrentVersionName);
//Convert raster to shapefile
oLayers.DeleteExistingShapefile(sInitWorkingDir, "RasToPoly");
oDataMan.ConvertRasterToPolygon2(sInitWorkingDir + @"\" + oGrid.CurrentVersionName,
sInitWorkingDir + @"\rastopoly");
//Get unique values from the shapefile - which are the cell_id's
pRasToPolyFeatClass = oLayers.OpenFeatureClassShapeFileWithNameObjects(sInitWorkingDir, "RasToPoly");
oLayers.ApplySpatialReference(pRasToPolyFeatClass, 27700);
iaUniqueCellIds = oTables2.GetUniqueValuesFromShapefile(pRasToPolyFeatClass, "GRIDCODE");
//Create Meanpoint shapefile (empty) for storage of mean points
pMeanPointFeatClass = oLayers.CreateShapefile(sInitWorkingDir, oGrid.MeanPointShapfileName, 27700,
esriGeometryType.esriGeometryPoint);
oDataMan.CreateMeanPointShapefile(pMeanPointFeatClass);
int iEastingIndex = pMeanPointFeatClass.Fields.FindField("EASTING");
int iNorthingIndex = pMeanPointFeatClass.Fields.FindField("NORTHING");
int i70Index = pMeanPointFeatClass.Fields.FindField("70");
int i80Index = pMeanPointFeatClass.Fields.FindField("80");
int i90Index = pMeanPointFeatClass.Fields.FindField("90");
int iLatIndex = pMeanPointFeatClass.Fields.FindField("LAT");
int iLongIndex = pMeanPointFeatClass.Fields.FindField("LONG");
int iCellNameIndex = pMeanPointFeatClass.Fields.FindField("CELLNAME");
int iCellIdIndex = pMeanPointFeatClass.Fields.FindField("CELLID");
int iBscRncIndex = pMeanPointFeatClass.Fields.FindField("BSC_RNC");
//Set the spatial reference for projecting each new point
ISpatialReferenceFactory2 pSpRefFactory = new SpatialReferenceEnvironmentClass();
ISpatialReference pSpRef = pSpRefFactory.CreateSpatialReference(27700);
pClutterFeatClass = oLayers.OpenFeatureClassShapeFileWithNameObjects(sInputDir, sClutterPtsShpFileName);
//Get raster for query
IRaster pQueryRaster = oLayers.OpenRaster(sInitWorkingDir, oGrid.CurrentVersionName);
//----------------START OF MAIN LOOP------------------//
for (int i = 0; i < iaUniqueCellIds.Length; i++)
{
DateTime oCurrDateTime;
TimeSpan runlen;
oCurrDateTime = DateTime.Now;
//Get polygon set per unique ID
pPolyFeatCursor = oSpatialOps.MakeSelectionFromShp(pRasToPolyFeatClass, "GRIDCODE = " + iaUniqueCellIds[i]);
//Get point array per polygon set
alPointFeatures = oSpatialOps.ClipPoints(pPolyFeatCursor, pClutterFeatClass);
if (alPointFeatures.Count == 0) continue;
//Get average wieghted point from point array
pWeightedPoint = oSpatialOps.MakeWeightedMeanPoint(alPointFeatures);
//Make space for point and store in shapefile
IFeature pWeightedFeature = pMeanPointFeatClass.CreateFeature();
pWeightedFeature.Shape = pWeightedPoint;
pWeightedPoint.Project(pSpRef);
//Store rest of attributes
pWeightedFeature.set_Value(iEastingIndex, pWeightedPoint.X);
pWeightedFeature.set_Value(iNorthingIndex, pWeightedPoint.Y);
//Calculate thresholds
pThesh = oSpatialOps.CalculateThresholds(pWeightedPoint, alPointFeatures);
//Create array to store 70, 80, 90 thresholds relate to 0,1,2 in array respectively
pWeightedFeature.set_Value(pMeanPointFeatClass.Fields.FindField("70"), pThesh.SeventyPercent);
pWeightedFeature.set_Value(pMeanPointFeatClass.Fields.FindField("80"), pThesh.EightyPercent);
pWeightedFeature.set_Value(pMeanPointFeatClass.Fields.FindField("90"), pThesh.NinetyPercent);
//Make long and lat values in table
pWGS84point.PutCoords(pWeightedPoint.X, pWeightedPoint.Y);
pWGS84point = MainGeoTasks.ConvertPointToWGS84(pWGS84point);
pWeightedFeature.set_Value(iLongIndex, pWGS84point.X);
pWeightedFeature.set_Value(iLatIndex, pWGS84point.Y);
//Insert Cell_old value in meanpoint.shp
sCellName = RasterOps.QueryRasterBySQL(pQueryRaster, "Cell_Old", "cell_id = " + Convert.ToString(iaUniqueCellIds[i]));
if (sCellName != "-9999") pWeightedFeature.set_Value(iCellNameIndex, sCellName);
//Insert CellId value in meanpoint.shp
pWeightedFeature.set_Value(iCellIdIndex, Convert.ToString(iaUniqueCellIds[i]));
//Need to switch query for either non-3G (BSC), or 3G (RNC)
if (oGrid.MeanPointShapfileName == "meanpointUMTS")
{
sRNC = RasterOps.QueryRasterBySQL(pQueryRaster, "Rnc", "cell_id = " + Convert.ToString(iaUniqueCellIds[i]));
if (sRNC != "-9999") pWeightedFeature.set_Value(iBscRncIndex, sRNC);
}
else
{
sBSC = RasterOps.QueryRasterBySQL(pQueryRaster, "Bsc", "cell_id = " + Convert.ToString(iaUniqueCellIds[i]));
if (sBSC != "-9999") pWeightedFeature.set_Value(iBscRncIndex, sBSC);
}
//Commit the point in the shapefile
pWeightedFeature.Store();
GC.Collect();
runlen = DateTime.Now - oCurrDateTime;
winOutputInfo.lblOutput.Text = i + @"/" + iaUniqueCellIds.Length;
winOutputInfo.txtOutput.Text = "Finished Cell ID " + iaUniqueCellIds[i] +
" in " + runlen.TotalSeconds.ToString() + " seconds";
winOutputInfo.Refresh();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + e.InnerException + "\n" + e.StackTrace);
winOutputInfo.Close();
Environment.Exit(0);
}
winOutputInfo.Close();
}