Hello, I have read some of the solution replied in discussion about the error on creating .rtg files. My problem is that, I created my own roads in GIS editor and then try to create a .rtg in routing explorer but the result is an error saying "object reference not set to an instance of the object". What should be the solution about this??
Creating .rtg Error
Hi Joseph,
Routing explorer can only be used to handle the data under DecimalDegree MapUnit, and it cannot deal with OneWay data.
So if your data require these features, please try to build RTG file like this:
RtgRoutingSource.GenerateRoutingData(rtgShapeFilePath, sourceShapeFilePath, routableShapeFilePath, BuildRoutingDataMode.Rebuild, GeographyUnit.Meter, DistanceUnit.Feet);
Attached is an utility for build your RTG file, you should want to did some changes for make it works for you.
Regards,
Don
BuildingOnewayRoadRoutingData.zip (13.4 KB)
Hello, I am aware about the comments in the file you sent me. I just add those needed references and run it but an error occur maybe because I need to edit some of the code but I don’t know where to start. I tried to analyze the codes but I’m afraid to change it to a worse result. Would you help me debug the codes. Since what only matters to me is to used the roads I have made in GIS editor for routing and I need a .rtg file for it,right? Here are the codes and the error:
usingSystem;usingSystem.Collections.ObjectModel;usingSystem.Windows.Forms;usingThinkGeo.MapSuite.Core;usingThinkGeo.MapSuite.Routing;usingSystem.IO;
namespaceBuildingOnewayRoadRoutingData{publicpartialclassForm1 : Form{privatestringsourceShapeFilePath;privatestringroutableShapeFilePath;privatestringrtgShapeFilePath;
privateFeatureSource routableShapeFileFeatureSource;
publicForm1(){InitializeComponent();
sourceShapeFilePath = txtSourceShpFilePath.Text;routableShapeFilePath = txtRoutableShpFile.Text;rtgShapeFilePath = Path.ChangeExtension(routableShapeFilePath,“rtg”);}
privatevoidbtnGenerateRoutableShapeFile_Click(objectsender, EventArgs e){ShapeFileFeatureSource originalFeatureSource =newShapeFileFeatureSource(sourceShapeFilePath);
// Todo: Set progress bar, can be removed if you would likeoriginalFeatureSource.Open();buildingPgbar.Maximum = originalFeatureSource.GetCount();originalFeatureSource.Close();buildingPgbar.Value = 0;buildingPgbar.Visible =true;btnGenerateRoutableShapeFile.Enabled =false;
RtgRoutingSource.GeneratingRoutableShapeFile +=newEventHandler<GeneratingRoutableShapeFileRoutingSourceEventArgs>(RtgRoutingSource_GeneratingRoutableShapeFile);RtgRoutingSource.GenerateRoutableShapeFile(sourceShapeFilePath, routableShapeFilePath, OverwriteMode.Overwrite);
btnGenerateRoutableShapeFile.Enabled =true;MessageBox.Show(“Routable Shape File has been Build! You can Start building routing index with it now.”);}
privatevoidRtgRoutingSource_GeneratingRoutableShapeFile(objectsender, GeneratingRoutableShapeFileRoutingSourceEventArgs e){if(buildingPgbar.Value < buildingPgbar.Maximum){buildingPgbar.Value += 1;}Application.DoEvents();}
privatevoidbtnBuildRoutingData_Click(objectsender, EventArgs e){routableShapeFileFeatureSource =newShapeFileFeatureSource(routableShapeFilePath);
routableShapeFileFeatureSource.Open();buildingPgbar.Maximum = routableShapeFileFeatureSource.GetCount();buildingPgbar.Value = 0;buildingPgbar.Visible =true;btnBuildRoutingData.Enabled =false;
RtgRoutingSource.BuildingRoutingData +=newEventHandler<BuildingRoutingDataRtgRoutingSourceEventArgs>(RtgRoutingSource_BuildingRoutingData);RtgRoutingSource.GenerateRoutingData(rtgShapeFilePath, sourceShapeFilePath, routableShapeFilePath, BuildRoutingDataMode.Rebuild, GeographyUnit.Meter, DistanceUnit.Feet);
btnBuildRoutingData.Enabled =true;MessageBox.Show(“Completed”);}
voidRtgRoutingSource_BuildingRoutingData(objectsender, BuildingRoutingDataRtgRoutingSourceEventArgs e){if(buildingPgbar.Value < buildingPgbar.Maximum){buildingPgbar.Value += 1;}Application.DoEvents();
if(!routableShapeFileFeatureSource.IsOpen){routableShapeFileFeatureSource.Open();}
Feature routableFeature = routableShapeFileFeatureSource.GetFeatureById(e.RouteSegment.FeatureId,newCollection<string>() {“ONE_WAY”});LineShape lineShape = GetLineShapeFromBaseShape(routableFeature);
// If it’s one-way roadif(routableFeature.ColumnValues[“ONE_WAY”].ToString() ==“1”){e.RouteSegment.StartPointAdjacentIds.Clear();}
// analysis adjacent one-way road featuresCollection<string> removedStartPointAdjacentIds = GetRemovedAdjacentIds(e.RouteSegment.StartPointAdjacentIds,newPointShape(lineShape.Vertices[0]));foreach(stringidinremovedStartPointAdjacentIds){e.RouteSegment.StartPointAdjacentIds.Remove(id);}Collection<string> removedEndPointAdjacentIds = GetRemovedAdjacentIds(e.RouteSegment.EndPointAdjacentIds,newPointShape(lineShape.Vertices[lineShape.Vertices.Count - 1]));foreach(stringidinremovedEndPointAdjacentIds){e.RouteSegment.EndPointAdjacentIds.Remove(id);}
// Todo: currently we haven’t process it, because we are unable to change the Huristic Value.// but we can uncomment below code if we are using the Dikjistra algorithm// e.RouteSegment.Weight /= (double)routableFeature.ColumnValues[“MPH”];}
privateCollection<string> GetRemovedAdjacentIds(Collection<string> adjacentIds, PointShape intersectingPoint){Collection<string> removedIds =newCollection<string>();foreach(stringidinadjacentIds){Feature adjacentFeature = routableShapeFileFeatureSource.GetFeatureById(id,newCollection<string>() {“ONE_WAY”});if(adjacentFeature.ColumnValues[“ONE_WAY”].ToString() ==“1”){LineShape adjacentLineShape = GetLineShapeFromBaseShape(adjacentFeature);doubledistanceFromAdjacentStartToIntersecting =newPointShape(adjacentLineShape.Vertices[0]).GetDistanceTo(intersectingPoint, GeographyUnit.Feet, DistanceUnit.Meter);doubledistanceFromAdjacentEndToIntersecting =newPointShape(adjacentLineShape.Vertices[adjacentLineShape.Vertices.Count - 1]).GetDistanceTo(intersectingPoint, GeographyUnit.Feet, DistanceUnit.Meter);if(distanceFromAdjacentEndToIntersecting < distanceFromAdjacentStartToIntersecting){removedIds.Add(id);}elseif(distanceFromAdjacentEndToIntersecting < distanceFromAdjacentStartToIntersecting){removedIds.Add(id);}}}returnremovedIds;}
privatestaticLineShape GetLineShapeFromBaseShape(Feature lineFeature){BaseShape baseShape = lineFeature.GetShape();
LineShape lineShape = baseShapeasLineShape;
if(lineShape ==null){MultilineShape lineShapes = ((MultilineShape)baseShape);Collection<Vertex> vertices =newCollection<Vertex>();
foreach(LineShape lineinlineShapes.Lines){for(inti = 0; i < line.Vertices.Count; i++){vertices.Add(line.Vertices<i>);}}
lineShape =newLineShape(vertices);lineShape.Id = baseShape.Id;lineShape.Tag = baseShape.Tag;}
returnlineShape;}
privatevoidbtnRoutableShpFile_Click(objectsender, EventArgs e){OpenFileDialog fd =newOpenFileDialog();fd.Filter =“Shape Files (*.shp)|*.shp”;if(fd.ShowDialog() == DialogResult.OK){txtRoutableShpFile.Text = fd.FileName;}}
privatevoidbtnSourceShpBrowse_Click(objectsender, EventArgs e){OpenFileDialog fd =newOpenFileDialog();fd.Filter =“Shape Files (*.shp)|*.shp”;if(fd.ShowDialog() == DialogResult.OK){txtSourceShpFilePath.Text = fd.FileName;}}}}

Hi Joseph,
Sorry I haven’t noticed your update here.
Have you fixed this problem? It looks you have another exception in today’s new topic.
Regards,
Don
Not yet but I already sent you the ShapeFile in other Topic I created!
Hi Joseph,
I will reply you on post thinkgeo.com/forums/MapSuite/tabid/143/aft/11858/Default.aspx , would you please check it there?
Best Regards
Summer