ラベル Editing Features and Attributes の投稿を表示しています。 すべての投稿を表示
ラベル Editing Features and Attributes の投稿を表示しています。 すべての投稿を表示

2014年2月20日木曜日

36 - GeoEXT を使用した WFS-T 12 - 選択したレイヤの編集 3

36-12-3 選択したレイヤのフィーチャと属性の編集
選択したレイヤのフィーチャの作成(draw)と編集(modify, delete)と、属性の編集ができるようにします。
3.2. Editing Features and Their Attributes(http://workshops.boundlessgeo.com/geoext/wfs/editing.html)の「Bonus Task」を参考に、一部を修正します。

---
function reconfigure(store, url) {
 var fields = [], columns = [], geometryName, geometryType;
 // regular expression to detect the geometry column
 var geomRegex = /gml:(Multi)?(Point|Line|Polygon|Surface|Geometry).*/;
 // mapping of xml schema data types to Ext JS data types
 var types = {
  "xsd:int": "int",
  "xsd:short": "int",
  "xsd:long": "int",
  "xsd:string": "string",
  "xsd:dateTime": "string",
  "xsd:double": "float",
  "xsd:decimal": "float",
  "Line": "Path",
  "Surface": "Polygon"
 };
 store.each(function(rec) {
  var type = rec.get("type");
  var name = rec.get("name");
  var match = geomRegex.exec(type);
  if (match) {
   // we found the geometry column
   geometryName = name;
   // Geometry type for the sketch handler:
   // match[2] is "Point", "Line", "Polygon", "Surface" or "Geometry"
   geometryType = types[match[2]] || match[2]; // 追加
  } else {
   // we have an attribute column
   fields.push({
    name: name,
    type: types[type]
   });
   columns.push({
    xtype: types[type] == "string" ?
     "gridcolumn" :
     "numbercolumn",
      dataIndex: name,
      header: name,
// ここから追加
      // textfield editor for strings, numberfield for others
      editor: {
       xtype: types[type] == "string" ?
        "textfield" :
        "numberfield"
      }
// ここまで
     });
    }
   });
   app.featureGrid.reconfigure(new GeoExt.data.FeatureStore({
    autoLoad: true,
    proxy: new GeoExt.data.ProtocolProxy({
     protocol: new OpenLayers.Protocol.WFS({
      url: url,
      version: "1.1.0",
      featureType: rawAttributeData.featureTypes[0].typeName,
      featureNS: rawAttributeData.targetNamespace,
//    srsName: "EPSG:4326",
      srsName: "EPSG:2451",
      geometryName: geometryName,
      maxFeatures: 250
     })
    }),
    fields: fields
   }), new Ext.grid.ColumnModel(columns));
   app.featureGrid.store.bind(vectorLayer);
   app.featureGrid.getSelectionModel().bind(vectorLayer);
// ここから追加
   // Set the correct sketch handler according to the geometryType
   drawControl.handler = new OpenLayers.Handler[geometryType](
    drawControl, drawControl.callbacks, drawControl.handlerOptions
   );
// ここまで
  }
  function setLayer(model, node) {
   if(!node || node.layer instanceof OpenLayers.Layer.Vector) {
    return;
   }
   vectorLayer.removeAllFeatures();
   app.featureGrid.reconfigure(
    new Ext.data.Store(),
    new Ext.grid.ColumnModel([])
   );
   var layer = node.layer;
   var url = layer.url.split("?")[0]; // the base url without params
   var schema = new GeoExt.data.AttributeStore({
    url: url,
    // request specific params
    baseParams: {
     "SERVICE": "WFS",
     "REQUEST": "DescribeFeatureType",
     "VERSION": "1.1.0",
     "TYPENAME": layer.params.LAYERS
    },
    autoLoad: true,
    listeners: {
     "load": function(store) {
       app.featureGrid.setTitle(layer.name);
       reconfigure(store, url);
     }
    }
   });
  }
  Ext.onReady(function() {
   app.tree.getSelectionModel().on(
    "selectionchange", setLayer
   );
  });

 </script>
---

参考 HTML ファイル

36 - GeoEXT を使用した WFS-T 10 - フィーチャとその属性の編集

36-10 フィーチャとその属性の編集
Developing OGC Compliant Web Applications with GeoExt の
3.2. Editing Features and Their Attributes(http://workshops.boundlessgeo.com/geoext/wfs/editing.html)を参考に WFS レイヤのフィーチャと属性を編集できるようにします。
続けて「ol017-nippon_bmi_akiruno_pgis.html」を使います。

---
items.push({
 xtype: "editorgrid", // 修正
 ref: "featureGrid",
 title: "Feature Table",
 region: "south",
 height: 150,
 sm: new GeoExt.grid.FeatureSelectionModel(),
 store: new GeoExt.data.FeatureStore({
  fields: [
   {name: "gid", type: "int"},
   {name: "id", type: "string"},
   {name: "orggilvl", type: "string"},
   {name: "orgmdid", type: "string"}, 
   {name: "category", type: "string"},
   {name: "flag", type: "string"},
   {name: "type", type: "string"},
   {name: "name", type: "string"},
   {name: "code", type: "string"}
  ],
  proxy: new GeoExt.data.ProtocolProxy({
   protocol: new OpenLayers.Protocol.WFS({
    url: "/geoserver/ows",
    version: "1.1.0",
    featureType: "akiruno_kukaku",
    featureNS: "http://www.myhome.net/npn",
    srsName: "EPSG:2451"
   }),
  })
  autoLoad: true
 }),
 columns: [
  {header: "gid", dataIndex: "gid"},
  {header: "id", dataIndex: "id", editor: {xtype: "textfield"}}, // editor: {xtype: "textfield"} 追加
  {header: "orggilvl", dataIndex: "orggilvl", editor: {xtype: "textfield"}},
  {header: "orgmdid", dataIndex: "orgmdid", editor: {xtype: "textfield"}},
  {header: "category", dataIndex: "category", editor: {xtype: "textfield"}},
  {header: "flag", dataIndex: "flag", editor: {xtype: "textfield"}},
  {header: "type", dataIndex: "type", editor: {xtype: "textfield"}},
  {header: "name", dataIndex: "name", editor: {xtype: "textfield"}},
  {header: "code", dataIndex: "code", editor: {xtype: "textfield"}}
 ],
 bbar: []
});
var vectorLayer = new OpenLayers.Layer.Vector("Editable features");
Ext.onReady(function() {
 app.mapPanel.map.addLayer(vectorLayer);
 app.featureGrid.store.bind(vectorLayer);
 app.featureGrid.getSelectionModel().bind(vectorLayer);
});
// コントロールの追加
var modifyControl = new OpenLayers.Control.ModifyFeature(
 vectorLayer, {autoActivate: true}
);
var drawControl = new OpenLayers.Control.DrawFeature(
 vectorLayer,
 OpenLayers.Handler.Polygon,
 {handlerOptions: {multi: true}}
);
controls.push(modifyControl, drawControl);
// グリッドが1行だけ選択されていることを確認
Ext.onReady(function() {
 var sm = app.featureGrid.getSelectionModel();
 sm.unbind();
 sm.bind(modifyControl.selectControl);
 sm.on("beforerowselect", function() { sm.clearSelections(); });
});
// "Delete" と "Create" ボタン追加
Ext.onReady(function() {
 var bbar = app.featureGrid.getBottomToolbar();
 bbar.add([{
  text: "Delete",
  handler: function() {
   app.featureGrid.getSelectionModel().each(function(rec) {
    var feature = rec.getFeature();
    modifyControl.unselectFeature(feature);
    vectorLayer.removeFeatures([feature]);
   });
  }
 }, new GeoExt.Action({
  control: drawControl,
  text: "Create",
  enableToggle: true
 })]);
 bbar.doLayout();
});

</script>
---