[mapguide-users] Set feature properties to a point
Mauricio Villablanca
mgvillablanca at yahoo.com
Mon Mar 4 18:23:58 PST 2013
After you have created the properties, you need to create your feature schema
and add data to the SDF.
Your code looks ok. I did something similar in PHP sometime ago.
See:
function makePoint($id, $x, $y) {
//makes a point
global $fp;
//debug ($fp, "makePoint():\r\n");
$name = "Node " . $id;
$propertyCollection = new MgPropertyCollection();
//name prop
$nameProperty = new MgStringProperty("NAME", $name);
$propertyCollection->Add($nameProperty);
//id prop
$idProperty = new MgInt32Property("ID", $id);
$propertyCollection->Add($idProperty);
//geom
$wktReaderWriter = new MgWktReaderWriter();
$agfReaderWriter = new MgAgfReaderWriter();
$geometry = $wktReaderWriter->Read("POINT XY ($x $y)");
$geometryByteReader = $agfReaderWriter->Write($geometry);
$geometryProperty = new MgGeometryProperty("GEOM", $geometryByteReader);
$propertyCollection->Add($geometryProperty);
return $propertyCollection;
}
function addNodesLayerToGroup($layer, $layerGroupName,
$layerGroupLegendLabel, &$map) {
global $fp;
// Adds a layer to a layer group. If necessary, it creates the layer
group.
// Get the layer group
$layerGroupCollection = $map->GetLayerGroups();
if ($layerGroupCollection->Contains($layerGroupName)) {
$layerGroup = $layerGroupCollection->GetItem($layerGroupName);
} else {
// It does not exist, so create it
$layerGroup = new MgLayerGroup($layerGroupName);
$layerGroup->SetVisible(true);
$layerGroup->SetDisplayInLegend(true);
$layerGroup->GetExpandInLegend();
$layerGroup->SetLegendLabel($layerGroupLegendLabel);
$layerGroupCollection->Add($layerGroup);
//place the group under "Filters"
//$parentGroup = $layerGroupCollection->GetItem("Filters");
//$layerGroup->SetGroup($parentGroup);
}
// Add the layer to the group
$layer->SetGroup($layerGroup);
//make the layer and its parents visible
$elem = $layer;
while ($elem != NULL) {
$elem->SetVisible(true);
$elem->SetDisplayInLegend(true);
$elem->GetExpandInLegend();
$elem = $elem->GetGroup();
}
}
function addNodesToMap($nodes) {
//adds nodes to the map
global $sessionId, $mapName, $fp;
try {
$userInfo = new MgUserInformation($sessionId);
$siteConnection = new MgSiteConnection();
$siteConnection->Open($userInfo);
$resourceService =
$siteConnection->CreateService(MgServiceType::ResourceService);
$featureService =
$siteConnection->CreateService(MgServiceType::FeatureService);
// Open the map
$map = new MgMap();
$map->Open($resourceService, $mapName);
// Create a feature source with point data.
// Create a feature class definition for the new feature source
$classDefinition = new MgClassDefinition();
$classDefinition->SetName("Points");
$classDefinition->SetDescription("Feature class with point data.");
$classDefinition->SetDefaultGeometryPropertyName("GEOM");
// Create a FeatId property
$featIdProp = "FeatId";
$identityProperty = new MgDataPropertyDefinition($featIdProp);
$identityProperty->SetDataType(MgPropertyType::Int32);
$identityProperty->SetAutoGeneration(true);
$identityProperty->SetReadOnly(true);
// Add the identity property to the class definition
$classDefinition->GetIdentityProperties()->Add($identityProperty);
$classDefinition->GetProperties()->Add($identityProperty);
// Create a name property
$nameProp = "NAME";
$nameProperty = new MgDataPropertyDefinition($nameProp);
$nameProperty->SetDataType(MgPropertyType::String);
// Add the name property to the class definition
$classDefinition->GetProperties()->Add($nameProperty);
// Create an ID property
$idProp = "ID";
$idProperty = new MgDataPropertyDefinition($idProp);
$idProperty->SetDataType(MgPropertyType::Int32);
// Add the name property to the class definition
$classDefinition->GetProperties()->Add($idProperty);
// Create a geometry property
$geometryProperty = new MgGeometricPropertyDefinition("GEOM");
$geometryProperty->SetGeometryTypes(MgFeatureGeometricType::Point);
// Add the geometry property to the class definition
$classDefinition->GetProperties()->Add($geometryProperty);
// Create a feature schema
$featureSchema = new MgFeatureSchema("PointSchema", "Point schema");
// Add the feature schema to the class definition
$featureSchema->GetClasses()->Add($classDefinition);
// Create the feature source
$featureSourceName =
'Library://SS_testing/Network_analysis/Data/Nodes.FeatureSource';
$resourceIdentifier = new MgResourceIdentifier($featureSourceName);
$srs = $map->GetMapSRS();
$spatialContext = "LL84";
$sdfParams = new MgCreateSdfParams($spatialContext, $srs,
$featureSchema);
$featureService->CreateFeatureSource($resourceIdentifier, $sdfParams);
// We need to add some data to the sdf before using it. The spatial
context reader must have an extent.
$batchPropertyCollection = new MgBatchPropertyCollection();
$wktReaderWriter = new MgWktReaderWriter();
$agfReaderWriter = new MgAgfReaderWriter();
$geometryFactory = new MgGeometryFactory();
for ($t=0; $t < count($nodes); $t++) {
$node = $nodes[$t];
$x = $node->X;
$y = $node->Y;
$id = $node->ID;
//$coordLL = transformCoordinate("CA83-VF", $spatialContext, $x, $y);
//$x = $coordLL->GetX();
//$y = $coordLL->GetY();
$propertyCollection = makePoint($id, $x, $y);
$batchPropertyCollection->Add($propertyCollection);
unset($propertyCollection);
}
// Add the batch property collection to the feature source
$cmd = new MgInsertFeatures("Points", $batchPropertyCollection);
$featureCommandCollection = new MgFeatureCommandCollection();
$featureCommandCollection->Add($cmd);
// Execute the "add" commands
$featureService->UpdateFeatures($resourceIdentifier,
$featureCommandCollection, false);
// Create a new layer
$factory = new LayerDefinitionFactory();
// Create a mark symbol
$resourceId =
'Library://Samples/Sheboygan/Symbols/BasicSymbols.SymbolLibrary';
$symbolName = 'PushPin';
$width = '11'; // unit = points
$height = '11'; // unit = points
$color = 'FFFF0000';
$markSymbol = $factory->CreateMarkSymbol($resourceId, $symbolName,
$width, $height, $color);
// Create a text symbol
$text = $idProp; //"ID"
$fontHeight="12";
$foregroundColor = 'FF000000';
$textSymbol = $factory->CreateTextSymbol($text, $fontHeight,
$foregroundColor);
// Create a point rule.
$legendLabel = 'Nodes';
$filter = '';
//$pointRule = $factory->CreatePointRule($legendLabel, $filter,
$textSymbol, $markSymbol);//don't use symbols
$pointRule = $factory->CreatePredefinedPointRule($legendLabel,
$filter, $text);
// Create a point type style.
$pointTypeStyle = $factory->CreatePointTypeStyle($pointRule);
// Create a scale range.
$minScale = '0';
$maxScale = '1000000000000';
$pointScaleRange = $factory->CreateScaleRange($minScale, $maxScale,
$pointTypeStyle);
// Create the layer definiton.
$featureName = 'PointSchema:Points';
$geometry = 'GEOM';
//$layerDefinition =
$factory->CreateLayerDefinition($featureSourceName, $featureName, $geometry,
$pointScaleRange);
//the original class doesn't use mappings for the features' layer
definition
$layerDefinition =
$factory->CreateNodeLayerDefinition($featureSourceName, $featureName,
$nameProp, $idProp, $featIdProp, $geometry, $pointScaleRange);
// Add the layer to the map
$newLayer = add_layer_definition_to_map($layerDefinition, "Points",
"Nodes", $sessionId, $resourceService, $map);
addNodesLayerToGroup($newLayer, "Analysis", "Analysis", $map);
// Turn on the visibility of this layer.
// (If the layer does not already exist in the map, it will be visible
by default when it is added.
// But if the user has already run this script, he or she may have set
the layer to be invisible.)
$layerCollection = $map->GetLayers();
if ($layerCollection->Contains("Points")) {
$pointsLayer =$layerCollection->GetItem("Points");
$pointsLayer->SetVisible(true);
$pointsLayer->SetSelectable(true);
}
// Save the map back to the session repository
$sessionIdName = "Session:$sessionId//$mapName.Map";
$sessionResourceID = new MgResourceIdentifier($sessionIdName);
$sessionResourceID->Validate();
$map->Save($resourceService, $sessionResourceID);
}
catch (MgException $e) {
debug ($fp, "\tMgException:" . $e->GetMessage() . "\r\n");
}
}
--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Set-feature-properties-to-a-point-tp5037772p5038429.html
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list