[OpenLayers-Users] Split a line feature with a MultiLineString
Tim Schaub
tschaub at opengeo.org
Tue Jan 5 16:55:31 EST 2010
Hello,
sendeman wrote:
> Hi All,
>
> I have an OpenLayers application that in which I have an XYZ-subsurface
> model residing in a MySQL database table. The model has a record for each
> voxel that has an X, Y and Z value and a value that indicates the soil type
> (e.g. 1 for clay, 2 for loam, etcetera).
>
> Using php, jQuery and OpenLayers I am able to retrieve all the relevant
> records for a location when a user clicks on the map. The php script returns
> a column with all the soillayers.
>
> Now what I also want to do is create the possibility for a user to draw a
> section line on the map and then get a vertical cross-section from the
> XYZ-model. To do this I mean to let the user draw a line and using the
> featureadded event I want to split the drawn line at every intersection with
> the grid of the XYZ model.
>
> I thought this should be possible with the
> http://dev.openlayers.org/docs/files/OpenLayers/Geometry/MultiLineString-js.html#OpenLayers.Geometry.MultiLineString.split
> split function for the MultiLineString . I built a WKT-string to create a
> MultiLineString for the grid. I have added the cell size as a variable to
> the script, so it will be reusable for models with different cell sizes. I
> want to intersect the drawn line with the created grid. This doesn't work. I
> get an error in Firebug:
>
> SectionGridFeature.split is not a function
> http://localhost/geoviewer/javascript/crosssection.js
> Line 75 ed. This would be the line stating:
> 'SectionGridFeature.split(varSections.features[0]);'
If you have build of the library that includes
OpenLayers.Geometry.MultiLineString, I'd say that your
SectionGridFeature is not a valid MultiLineString.
If you're still working through this problem, I'd recommend dumping your
well-known text into this example and make sure it parses:
http://openlayers.org/dev/examples/vector-formats.html
(select WKT)
Tim
>
>
> For the script I made, see below. This doesn't include the entire openlayers
> script I built, but just the section I'm using for the creation of the
> cross-section.
>
> My question is: how can I make the split function work? I fear something may
> be wrong with the MultiLineString...
>
> Best regards,
> Martijn Senden.
>
>
> // JavaScript Document for drawing profile lines that are used for drawing
> // cross-sections through the Rockworks XYZ-model in the MWH Geoviewer
> // V. 1.0 By Martijn Senden, MWH B.V., The Netherlands
>
> // Project parameters
> var CellSize = 10; //Size of the grid cells in the XYZ-model
>
> // Stylemap for Cross-section line
> function initCrossSection() {
> var StyleCustomCrossSectionLine = new OpenLayers.StyleMap({
> "default": new OpenLayers.Style({
> strokeColor: "#ffff00",
> strokeWidth: 2.5
> })
> });
> var StyleCustomGrid = new OpenLayers.StyleMap({
> "default": new OpenLayers.Style({
> strokeColor: "#ffff00",
> fillColor: "#ffbb00",
> strokeWidth: 2.5
> })
> });
>
> // Addition of cross-section layer to map and enter drawing mode
> var varSections = new OpenLayers.Layer.Vector("Section Line", {
> visibility: true, styleMap: StyleCustomCrossSectionLine});
> map.addLayer(varSections);
> var varSection = new OpenLayers.Control.DrawFeature(
> varSections,
> OpenLayers.Handler.Path,
> {displayClass: 'olControlDrawFeaturePolygon'}
> );
> varSections.events.on({
> //Delete previous line before new feature is added
> "beforefeatureadded": function(evt) {
> var i, len, toDestroy = []
> for(i=0,len=varSections.features.length; i<len; i++) {
> if(varSections.features[i] != evt.feature) {
> toDestroy.push(varSections.features[i]);
> }
> }
> varSections.destroyFeatures(toDestroy);
> },
> //When new feature is added:
> "featureadded": function(evt) {
>
> //Get Xmin, Xmax, Ymin and Ymax for the new grid used for intersection
> var SectionBounds =
> varSections.features[0].geometry.getBounds().clone();
> var vXmin = RoundToGrid(SectionBounds.left,CellSize,"Up");
> var vXmax = RoundToGrid(SectionBounds.right,CellSize,"Down");
> var vYmin = RoundToGrid(SectionBounds.bottom,CellSize,"Up");
> var vYmax = RoundToGrid(SectionBounds.top,CellSize,"Down");
>
> //Create intersection polygon grid
> var varSectionGrid = new OpenLayers.Layer.Vector("Section Gridpunten",
> {visibility: true, styleMap: StyleCustomGrid});
> parser = new OpenLayers.Format.WKT();
>
> var wkt = " MULTILINESTRING(";
>
> //Draw vertical lines
> for (y = vYmin; y < vYmax; y = y + CellSize) {
> wkt = wkt+"("+vXmin+" "+y+")("+vXmax+" "+y+")";
> }
> //Draw horizontal lines
> for (x = vXmin; x < vXmax; x = x + CellSize) {
> wkt = wkt+"("+x+" "+vYmin+")("+x+" "+vYmax+")";
> }
> var SectionGridGeometry = parser.read(wkt);
> var SectionGridFeature = new
> OpenLayers.Feature.Vector(SectionGridGeometry);
> varSectionGrid.addFeatures(SectionGridFeature);
>
> //Split the Section line with the temporary grid
> SectionGridFeature.split(varSections.features[0]);
> varSectionGrid.features[0].geometry.split(varSections.features[0]);
>
> //Construct Json-string from the coordinates of the vertices of the
> drawn section line
> varSectionCoords = '{';
> for (var i = 0; i < varSections.features[0].geometry.components.length
> - 1; i++) {
> varSectionCoords = varSectionCoords + '"point' + '":{"x":"' +
> varSections.features[0].geometry.components[i].x + '","y":"' +
> varSections.features[0].geometry.components[i].y + '"},';
> }
> varSectionCoords = varSectionCoords + '"point' + '":{"x":"' +
> varSections.features[0].geometry.components[varSections.features[0].geometry.components.length
> - 1].x + '","y":"' +
> varSections.features[0].geometry.components[varSections.features[0].geometry.components.length
> - 1].y + '"}}';
> }
> });
> map.addControl(varSection);
> varSection.activate();
> }
>
> function RoundToGrid(Value, GridResolution, Type) {
> switch (Type) {
> case "Up":
> return Math.ceil(Value / GridResolution)*GridResolution;
> break;
> case "Down":
> return Math.floor(Value / GridResolution)*GridResolution;
> break;
> case "Round":
> return Math.round(Value / GridResolution)*GridResolution;
> break;
> }
> }
>
--
Tim Schaub
OpenGeo - http://opengeo.org
Expert service straight from the developers.
More information about the Users
mailing list