[mapguide-users] Add a new point to layer ( digitizing )

Rahul sharma rahulonline22 at yahoo.com
Tue Apr 29 20:18:22 EDT 2008


Hi 
I am using Map Guide Open Source and map guide studio 2008 . for my project
i am trying to digitize a point to the map. i have created a javascript
which pass the coordinates and on formsubmit invoke the php. The code should
creates a layer and add it back to the map. 
But i have problem displaying the point and its not doing anything any
suggession or help wil be greatful 
the code is as under 



<?php 
require_once('../common/common.php'); 
require_once($webExtensionsDirectory .
'www/mapviewerphp/layerdefinitionfactory.php'); 
require_once('layer_functions.php'); 

  $args = ($_SERVER['REQUEST_METHOD'] == "POST")?$_POST: $_GET; 
  $sessionId = $args['SESSION']; 
  $mapName = $args['MAPNAME']; 
// coordiantes of cliked point of map (user can add new point to map) 
// it comes form javascript , 

$x=$_GET['X']; 
$y=$_GET['Y']; 
try 
{ 
    //---------------------------------------------------// 
    // Initialize 
    MgInitializeWebTier($webconfigFilePath); 
    $sessionId = ($_SERVER['REQUEST_METHOD'] == "POST")?$_POST['SESSION']:
$_GET['SESSION']; 
    $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. 
    // (The Sheboygan sample data does not contain such data, 
    // so we'll create it.) 
    
        // 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 an identify property 
        $identityProperty = new MgDataPropertyDefinition("KEY"); 
        $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 
        $nameProperty = new MgDataPropertyDefinition("NAME"); 
        $nameProperty->SetDataType(MgPropertyType::String); 
        // Add the name property to the class definition 
        $classDefinition->GetProperties()->Add($nameProperty); 
        
        // Create a geometry property 
        $geometryProperty = new MgGeometricPropertyDefinition("GEOM"); 
       
$geometryProperty->SetGeometryTypes(MgFeatureGeometricType::Surface); 
        // 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 =
"Session:$sessionId//TemporaryPoints.FeatureSource"; 
        //$featureSourceName =
"Library://EelGrass/DevGuide/Data/points.FeatureSource"; 
        $resourceIdentifier = new MgResourceIdentifier($featureSourceName); 
        $wkt = $map->GetMapSRS(); 
        //$wkt =
"LOCALCS[\"*XY-MT*\",LOCAL_DATUM[\"*X-Y*\",10000],UNIT[\"Meter\",
1],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH]]"; 
        $sdfParams = new MgCreateSdfParams("ArbitraryXY", $wkt,
$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(); 

    // Create some point and display them using Make Point function 
        $propertyCollection = MakePoint("PointA", $x,$y); 
        $batchPropertyCollection->Add($propertyCollection); 
        unset($propertyCollection); 
        
        $propertyCollection = MakePoint("Point B", 237923.91,5842975.50); 
        $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://EelGrass/Symbols/BasicSymbols.SymbolLibrary'; 
    $symbolName = 'Point'; 
    $width = '24';  // unit = points 
    $height = '24'; // unit = points 
    $color = 'FFFF0000'; 
    $markSymbol = $factory->CreateMarkSymbol($resourceId, $symbolName,
$width, $height, $color);   
    
    // Create a text symbol 
    $text = "ID"; 
    $fontHeight="12"; 
    $foregroundColor = 'FF000000'; 
    $textSymbol = $factory->CreateTextSymbol($text, $fontHeight,
$foregroundColor); 

    // Create a point rule. 
    $legendLabel = 'trees'; 
    $filter = ''; 
    $pointRule = $factory->CreatePointRule($legendLabel,$filter,
$textSymbol, $markSymbol); 
      
    // 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); 
    
        //---------------------------------------------------// 
   // Add layer permanently to the map using XML writer 
        $byteSource = new MgByteSource($layerDefinition,
strlen($layerDefinition)); 
        $byteSource->SetMimeType(MgMimeType::Xml); 
        $resourceIden = new
MgResourceIdentifier("Library://Project/Data/Points.FeatureSource"); 
        $resourceService->SetResource($resourceIden,
$byteSource->GetReader(), null); 
    
        // Add the layer to the map 
    $newLayer = add_layer_definition_to_map($layerDefinition, "Points",
"Points of Interest", $sessionId, $resourceService, $map); 
    add_layer_to_group($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); 
    } 

    //---------------------------------------------------// 
    //  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) 
{ 
        echo "<script language=\"javascript\" type=\"text/javascript\"> \n"; 
        echo "    alert(\" " . $e->GetMessage() . " \"); \n"; 
        echo "</script> \n"; 
} 

/////////////////////////////////////////////////////////////////////////////////// 
function MakePoint($name, $x, $y) 
{ 
        $propertyCollection = new MgPropertyCollection(); 
        $nameProperty = new MgStringProperty("NAME", $name); 
        $propertyCollection->Add($nameProperty); 
        
        $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; 
} 
?> 

</body> 
</html>
-- 
View this message in context: http://www.nabble.com/Add-a-new-point-to-layer-%28-digitizing-%29-tp16973289p16973289.html
Sent from the MapGuide Users mailing list archive at Nabble.com.



More information about the mapguide-users mailing list