001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on 14 October 2002, 15:50
017: */
018: package org.geotools.styling;
019:
020: import org.geotools.factory.CommonFactoryFinder;
021: import org.geotools.factory.GeoTools;
022: import org.opengis.filter.Filter;
023: import org.opengis.filter.FilterFactory;
024: import org.opengis.filter.expression.Expression;
025:
026: /**
027: * Factory for creating Styles. All style elements are returned as Interfaces
028: * from org.geotools.core as opposed to Implementations from
029: * org.geotools.defaultcore.
030: *
031: * @author iant
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/styling/StyleFactoryImpl.java $
033: * @version $Id: StyleFactoryImpl.java 25459 2007-05-08 05:19:25Z jgarnett $
034: */
035: public class StyleFactoryImpl extends AbstractStyleFactory implements
036: StyleFactory2 {
037:
038: private FilterFactory filterFactory;
039:
040: public StyleFactoryImpl() {
041: this (CommonFactoryFinder.getFilterFactory(GeoTools
042: .getDefaultHints()));
043: }
044:
045: protected StyleFactoryImpl(FilterFactory factory) {
046: filterFactory = factory;
047: }
048:
049: public Style createStyle() {
050: return new StyleImpl();
051: }
052:
053: public NamedStyle createNamedStyle() {
054: return new NamedStyleImpl();
055: }
056:
057: public PointSymbolizer createPointSymbolizer() {
058: return new PointSymbolizerImpl();
059: }
060:
061: public PointSymbolizer createPointSymbolizer(Graphic graphic,
062: String geometryPropertyName) {
063: PointSymbolizer pSymb = new PointSymbolizerImpl();
064: pSymb.setGeometryPropertyName(geometryPropertyName);
065: pSymb.setGraphic(graphic);
066:
067: return pSymb;
068: }
069:
070: public PolygonSymbolizer createPolygonSymbolizer() {
071: return new PolygonSymbolizerImpl();
072: }
073:
074: public PolygonSymbolizer createPolygonSymbolizer(Stroke stroke,
075: Fill fill, String geometryPropertyName) {
076: PolygonSymbolizer pSymb = new PolygonSymbolizerImpl();
077: pSymb.setGeometryPropertyName(geometryPropertyName);
078: pSymb.setStroke(stroke);
079: pSymb.setFill(fill);
080:
081: return pSymb;
082: }
083:
084: public LineSymbolizer createLineSymbolizer() {
085: return new LineSymbolizerImpl();
086: }
087:
088: public LineSymbolizer createLineSymbolizer(Stroke stroke,
089: String geometryPropertyName) {
090: LineSymbolizer lSymb = new LineSymbolizerImpl();
091: lSymb.setGeometryPropertyName(geometryPropertyName);
092: lSymb.setStroke(stroke);
093:
094: return lSymb;
095: }
096:
097: public TextSymbolizer createTextSymbolizer() {
098: return new TextSymbolizerImpl();
099: }
100:
101: public TextSymbolizer createTextSymbolizer(Fill fill, Font[] fonts,
102: Halo halo, Expression label, LabelPlacement labelPlacement,
103: String geometryPropertyName) {
104: TextSymbolizer tSymb = new TextSymbolizerImpl();
105: tSymb.setFill(fill);
106: tSymb.setFonts(fonts);
107: tSymb.setGeometryPropertyName(geometryPropertyName);
108:
109: tSymb.setHalo(halo);
110: tSymb.setLabel(label);
111: tSymb.setPlacement(labelPlacement);
112:
113: return tSymb;
114: }
115:
116: public TextSymbolizer2 createTextSymbolizer(Fill fill,
117: Font[] fonts, Halo halo, Expression label,
118: LabelPlacement labelPlacement, String geometryPropertyName,
119: Graphic graphic) {
120: TextSymbolizer2 tSymb = new TextSymbolizerImpl();
121: tSymb.setFill(fill);
122: tSymb.setFonts(fonts);
123: tSymb.setGeometryPropertyName(geometryPropertyName);
124:
125: tSymb.setHalo(halo);
126: tSymb.setLabel(label);
127: tSymb.setPlacement(labelPlacement);
128: tSymb.setGraphic(graphic);
129:
130: return tSymb;
131: }
132:
133: public Extent createExtent(String name, String value) {
134: Extent extent = new ExtentImpl();
135: extent.setName(name);
136: extent.setValue(value);
137:
138: return extent;
139: }
140:
141: public FeatureTypeConstraint createFeatureTypeConstraint(
142: String featureTypeName, Filter filter, Extent[] extents) {
143: FeatureTypeConstraint constraint = new FeatureTypeConstraintImpl();
144: constraint.setFeatureTypeName(featureTypeName);
145: constraint.setFilter(filter);
146: constraint.setExtents(extents);
147:
148: return constraint;
149: }
150:
151: public LayerFeatureConstraints createLayerFeatureConstraints(
152: FeatureTypeConstraint[] featureTypeConstraints) {
153: LayerFeatureConstraints constraints = new LayerFeatureConstraintsImpl();
154: constraints.setFeatureTypeConstraints(featureTypeConstraints);
155:
156: return constraints;
157: }
158:
159: public FeatureTypeStyle createFeatureTypeStyle() {
160: return new FeatureTypeStyleImpl();
161: }
162:
163: public FeatureTypeStyle createFeatureTypeStyle(Rule[] rules) {
164: return new FeatureTypeStyleImpl(rules);
165: }
166:
167: public Rule createRule() {
168: return new RuleImpl();
169: }
170:
171: public ImageOutline createImageOutline(Symbolizer symbolizer) {
172: ImageOutline outline = new ImageOutlineImpl();
173: outline.setSymbolizer(symbolizer);
174:
175: return outline;
176: }
177:
178: /**
179: * A convienice method to make a simple stroke
180: *
181: * @param color the color of the line
182: * @param width the width of the line
183: *
184: * @return the stroke object
185: *
186: * @see org.geotools.stroke
187: */
188: public Stroke createStroke(Expression color, Expression width) {
189: return createStroke(color, width, filterFactory.literal(1.0));
190: }
191:
192: /**
193: * A convienice method to make a simple stroke
194: *
195: * @param color the color of the line
196: * @param width The width of the line
197: * @param opacity The opacity of the line
198: *
199: * @return The stroke
200: *
201: * @see org.geotools.stroke
202: */
203: public Stroke createStroke(Expression color, Expression width,
204: Expression opacity) {
205: return createStroke(color, width, opacity, filterFactory
206: .literal("miter"), filterFactory.literal("butt"), null,
207: filterFactory.literal(0.0), null, null);
208: }
209:
210: /**
211: * creates a stroke
212: *
213: * @param color The color of the line
214: * @param width The width of the line
215: * @param opacity The opacity of the line
216: * @param lineJoin - the type of Line joint
217: * @param lineCap - the type of line cap
218: * @param dashArray - an array of floats describing the dashes in the line
219: * @param dashOffset - where in the dash array to start drawing from
220: * @param graphicFill - a graphic object to fill the line with
221: * @param graphicStroke - a graphic object to draw the line with
222: *
223: * @return The completed stroke.
224: *
225: * @throws IllegalArgumentException DOCUMENT ME!
226: *
227: * @see org.geotools.stroke
228: */
229: public Stroke createStroke(Expression color, Expression width,
230: Expression opacity, Expression lineJoin,
231: Expression lineCap, float[] dashArray,
232: Expression dashOffset, Graphic graphicFill,
233: Graphic graphicStroke) {
234: Stroke stroke = new StrokeImpl();
235:
236: if (color == null) {
237: //use default
238: color = Stroke.DEFAULT.getColor();
239: }
240: stroke.setColor(color);
241:
242: if (width == null) {
243: //use default
244: width = Stroke.DEFAULT.getWidth();
245: }
246: stroke.setWidth(width);
247:
248: if (opacity == null) {
249: opacity = Stroke.DEFAULT.getOpacity();
250: ;
251: }
252: stroke.setOpacity(opacity);
253:
254: if (lineJoin == null) {
255: lineJoin = Stroke.DEFAULT.getLineJoin();
256: }
257: stroke.setLineJoin(lineJoin);
258:
259: if (lineCap == null) {
260: lineCap = Stroke.DEFAULT.getLineCap();
261: }
262:
263: stroke.setLineCap(lineCap);
264: stroke.setDashArray(dashArray);
265: stroke.setDashOffset(dashOffset);
266: stroke.setGraphicFill(graphicFill);
267: stroke.setGraphicStroke(graphicStroke);
268:
269: return stroke;
270: }
271:
272: public Fill createFill(Expression color,
273: Expression backgroundColor, Expression opacity,
274: Graphic graphicFill) {
275: Fill fill = new FillImpl();
276:
277: if (color == null) {
278: color = Fill.DEFAULT.getColor();
279: }
280: fill.setColor(color);
281: if (backgroundColor == null) {
282: backgroundColor = Fill.DEFAULT.getBackgroundColor();
283: }
284: fill.setBackgroundColor(backgroundColor);
285:
286: if (opacity == null) {
287: opacity = Fill.DEFAULT.getOpacity();
288: }
289:
290: // would be nice to check if this was within bounds but we have to wait until use since it may depend on an attribute
291: fill.setOpacity(opacity);
292: fill.setGraphicFill(graphicFill);
293:
294: return fill;
295: }
296:
297: public Fill createFill(Expression color, Expression opacity) {
298: return createFill(color, null, opacity, null);
299: }
300:
301: public Fill createFill(Expression color) {
302: return createFill(color, null, filterFactory.literal(1.0), null);
303: }
304:
305: public Mark createMark(Expression wellKnownName, Stroke stroke,
306: Fill fill, Expression size, Expression rotation) {
307: Mark mark = new MarkImpl();
308:
309: if (wellKnownName == null) {
310: throw new IllegalArgumentException(
311: "WellKnownName can not be null in mark");
312: }
313:
314: mark.setWellKnownName(wellKnownName);
315: mark.setStroke(stroke);
316: mark.setFill(fill);
317:
318: if (size == null) {
319: throw new IllegalArgumentException(
320: "Size can not be null in mark");
321: }
322:
323: mark.setSize(size);
324:
325: if (rotation == null) {
326: throw new IllegalArgumentException(
327: "Rotation can not be null in mark");
328: }
329:
330: mark.setRotation(rotation);
331:
332: return mark;
333: }
334:
335: public Mark getSquareMark() {
336: Mark mark = createMark(filterFactory.literal("Square"),
337: getDefaultStroke(), getDefaultFill(), filterFactory
338: .literal(6), filterFactory.literal(0));
339:
340: return mark;
341: }
342:
343: public Mark getCircleMark() {
344: Mark mark = getDefaultMark();
345: mark.setWellKnownName(filterFactory.literal("Circle"));
346:
347: return mark;
348: }
349:
350: public Mark getCrossMark() {
351: Mark mark = getDefaultMark();
352: mark.setWellKnownName(filterFactory.literal("Cross"));
353:
354: return mark;
355: }
356:
357: public Mark getXMark() {
358: Mark mark = getDefaultMark();
359: mark.setWellKnownName(filterFactory.literal("X"));
360:
361: return mark;
362: }
363:
364: public Mark getTriangleMark() {
365: Mark mark = getDefaultMark();
366: mark.setWellKnownName(filterFactory.literal("Triangle"));
367:
368: return mark;
369: }
370:
371: public Mark getStarMark() {
372: Mark mark = getDefaultMark();
373: mark.setWellKnownName(filterFactory.literal("Star"));
374:
375: return mark;
376: }
377:
378: public Mark createMark() {
379: Mark mark = new MarkImpl();
380:
381: return mark;
382: }
383:
384: public Graphic createGraphic(ExternalGraphic[] externalGraphics,
385: Mark[] marks, Symbol[] symbols, Expression opacity,
386: Expression size, Expression rotation) {
387: Graphic graphic = new GraphicImpl();
388:
389: symbols = symbols != null ? symbols : new Symbol[0];
390: graphic.setSymbols(symbols);
391:
392: externalGraphics = externalGraphics != null ? externalGraphics
393: : new ExternalGraphic[0];
394: graphic.setExternalGraphics(externalGraphics);
395:
396: marks = marks != null ? marks : new Mark[0];
397: graphic.setMarks(marks);
398:
399: if (opacity == null) {
400: opacity = Graphic.DEFAULT.getOpacity();
401: }
402: graphic.setOpacity(opacity);
403:
404: if (size == null) {
405: size = Graphic.DEFAULT.getSize();
406: }
407: graphic.setSize(size);
408:
409: if (rotation == null) {
410: rotation = Graphic.DEFAULT.getRotation();
411: }
412:
413: graphic.setRotation(rotation);
414:
415: return graphic;
416: }
417:
418: public ExternalGraphic createExternalGraphic(String uri,
419: String format) {
420: ExternalGraphic extg = new ExternalGraphicImpl();
421: extg.setURI(uri);
422: extg.setFormat(format);
423:
424: return extg;
425: }
426:
427: public ExternalGraphic createExternalGraphic(java.net.URL url,
428: String format) {
429: ExternalGraphic extg = new ExternalGraphicImpl();
430: extg.setLocation(url);
431: extg.setFormat(format);
432:
433: return extg;
434: }
435:
436: public Font createFont(Expression fontFamily, Expression fontStyle,
437: Expression fontWeight, Expression fontSize) {
438: Font font = new FontImpl();
439:
440: if (fontFamily == null) {
441: throw new IllegalArgumentException(
442: "Null font family specified");
443: }
444: font.setFontFamily(fontFamily);
445:
446: if (fontSize == null) {
447: throw new IllegalArgumentException(
448: "Null font size specified");
449: }
450:
451: font.setFontSize(fontSize);
452:
453: if (fontStyle == null) {
454: throw new IllegalArgumentException(
455: "Null font Style specified");
456: }
457:
458: font.setFontStyle(fontStyle);
459:
460: if (fontWeight == null) {
461: throw new IllegalArgumentException(
462: "Null font weight specified");
463: }
464:
465: font.setFontWeight(fontWeight);
466:
467: return font;
468: }
469:
470: // public LinePlacement createLinePlacement(){
471: // return new LinePlacementImpl();
472: // }
473: public LinePlacement createLinePlacement(Expression offset) {
474: LinePlacement linep = new LinePlacementImpl();
475: linep.setPerpendicularOffset(offset);
476:
477: return linep;
478: }
479:
480: // public PointPlacement createPointPlacement(){
481: // return new PointPlacementImpl();
482: // }
483: public PointPlacement createPointPlacement(AnchorPoint anchorPoint,
484: Displacement displacement, Expression rotation) {
485: PointPlacement pointp = new PointPlacementImpl();
486: pointp.setAnchorPoint(anchorPoint);
487: pointp.setDisplacement(displacement);
488: pointp.setRotation(rotation);
489:
490: return pointp;
491: }
492:
493: public AnchorPoint createAnchorPoint(Expression x, Expression y) {
494: AnchorPoint anchorPoint = new AnchorPointImpl();
495: anchorPoint.setAnchorPointX(x);
496: anchorPoint.setAnchorPointY(y);
497:
498: return anchorPoint;
499: }
500:
501: public Displacement createDisplacement(Expression x, Expression y) {
502: Displacement displacement = new DisplacementImpl();
503: displacement.setDisplacementX(x);
504: displacement.setDisplacementY(y);
505:
506: return displacement;
507: }
508:
509: public Halo createHalo(Fill fill, Expression radius) {
510: Halo halo = new HaloImpl();
511: halo.setFill(fill);
512: halo.setRadius(radius);
513:
514: return halo;
515: }
516:
517: public Fill getDefaultFill() {
518: Fill fill = new FillImpl();
519:
520: try {
521: fill.setColor(filterFactory.literal("#808080"));
522: fill.setOpacity(filterFactory.literal(new Double(1.0)));
523: } catch (org.geotools.filter.IllegalFilterException ife) {
524: throw new RuntimeException("Error creating fill", ife);
525: }
526:
527: return fill;
528: }
529:
530: public LineSymbolizer getDefaultLineSymbolizer() {
531: return createLineSymbolizer(getDefaultStroke(), null);
532: }
533:
534: public Mark getDefaultMark() {
535: return getSquareMark();
536: }
537:
538: public PointSymbolizer getDefaultPointSymbolizer() {
539: return createPointSymbolizer(createDefaultGraphic(), null);
540: }
541:
542: public PolygonSymbolizer getDefaultPolygonSymbolizer() {
543: return createPolygonSymbolizer(getDefaultStroke(),
544: getDefaultFill(), null);
545: }
546:
547: public Stroke getDefaultStroke() {
548: try {
549: Stroke stroke = createStroke(filterFactory
550: .literal("#000000"), filterFactory
551: .literal(new Integer(1)));
552:
553: stroke.setDashOffset(filterFactory.literal(new Integer(0)));
554: stroke.setLineCap(filterFactory.literal("butt"));
555: stroke.setLineJoin(filterFactory.literal("miter"));
556: stroke.setOpacity(filterFactory.literal(new Integer(1)));
557:
558: return stroke;
559: } catch (org.geotools.filter.IllegalFilterException ife) {
560: //we should never be in here
561: throw new RuntimeException("Error creating stroke", ife);
562: }
563: }
564:
565: public Style getDefaultStyle() {
566: Style style = createStyle();
567:
568: return style;
569: }
570:
571: /**
572: * Creates a default Text Symbolizer, using the defaultFill, defaultFont
573: * and defaultPointPlacement, Sets the geometry attribute name to be
574: * geometry:text. No Halo is set. <b>The label is not set</b>
575: *
576: * @return A default TextSymbolizer
577: */
578: public TextSymbolizer getDefaultTextSymbolizer() {
579: return createTextSymbolizer(getDefaultFill(),
580: new Font[] { getDefaultFont() }, null, null,
581: getDefaultPointPlacement(), "geometry:text");
582: }
583:
584: /**
585: * Creates a defaultFont which is valid on all machines. The font is of
586: * size 10, Style and Weight normal and uses a serif font.
587: *
588: * @return the default Font
589: *
590: * @throws RuntimeException DOCUMENT ME!
591: */
592: public Font getDefaultFont() {
593: Font font = new FontImpl();
594:
595: try {
596: font.setFontSize(filterFactory.literal(new Integer(10)));
597: font.setFontStyle(filterFactory.literal("normal"));
598: font.setFontWeight(filterFactory.literal("normal"));
599: font.setFontFamily(filterFactory.literal("Serif"));
600: } catch (org.geotools.filter.IllegalFilterException ife) {
601: throw new RuntimeException("Error creating font", ife);
602: }
603:
604: return font;
605: }
606:
607: public Graphic createDefaultGraphic() {
608: Graphic graphic = new GraphicImpl();
609:
610: graphic.setSize(filterFactory.literal(6));
611: graphic.setOpacity(filterFactory.literal(1.0));
612: graphic.setRotation(filterFactory.literal(0.0));
613:
614: return graphic;
615: }
616:
617: public Graphic getDefaultGraphic() {
618: return createDefaultGraphic();
619: }
620:
621: /**
622: * returns a default PointPlacement with a 0,0 anchorPoint and a
623: * displacement of 0,0 and a rotation of 0
624: *
625: * @return a default PointPlacement.
626: */
627: public PointPlacement getDefaultPointPlacement() {
628: return this .createPointPlacement(this .createAnchorPoint(
629: filterFactory.literal(0), filterFactory.literal(0.5)),
630: this .createDisplacement(filterFactory.literal(0),
631: filterFactory.literal(0)), filterFactory
632: .literal(0));
633: }
634:
635: public RasterSymbolizer createRasterSymbolizer() {
636: return new RasterSymbolizerImpl();
637: }
638:
639: public RasterSymbolizer createRasterSymbolizer(
640: String geometryPropertyName, Expression opacity,
641: ChannelSelection channel, Expression overlap,
642: ColorMap colorMap, ContrastEnhancement cenhancement,
643: ShadedRelief relief, Symbolizer outline) {
644: RasterSymbolizer rastersym = new RasterSymbolizerImpl();
645:
646: if (geometryPropertyName != null) {
647: rastersym.setGeometryPropertyName(geometryPropertyName);
648: }
649:
650: if (opacity != null) {
651: rastersym.setOpacity(opacity);
652: }
653:
654: if (channel != null) {
655: rastersym.setChannelSelection(channel);
656: }
657:
658: if (overlap != null) {
659: rastersym.setOverlap(overlap);
660: }
661:
662: if (colorMap != null) {
663: rastersym.setColorMap(colorMap);
664: }
665:
666: if (cenhancement != null) {
667: rastersym.setContrastEnhancement(cenhancement);
668: }
669:
670: if (relief != null) {
671: rastersym.setShadedRelief(relief);
672: }
673:
674: if (outline != null) {
675: rastersym.setImageOutline(outline);
676: }
677:
678: return rastersym;
679: }
680:
681: public RasterSymbolizer getDefaultRasterSymbolizer() {
682: return createRasterSymbolizer("geom", filterFactory
683: .literal(1.0), null, null, null, null, null, null);
684: }
685:
686: public ChannelSelection createChannelSelection(
687: SelectedChannelType[] channels) {
688: ChannelSelection channelSel = new ChannelSelectionImpl();
689:
690: if ((channels != null) && (channels.length > 0)) {
691: channelSel.setSelectedChannels(channels);
692: }
693:
694: return channelSel;
695: }
696:
697: public ColorMap createColorMap() {
698: return new ColorMapImpl();
699: }
700:
701: public ColorMapEntry createColorMapEntry() {
702: return new ColorMapEntryImpl();
703: }
704:
705: public ContrastEnhancement createContrastEnhancement() {
706: return new ContrastEnhancementImpl();
707: }
708:
709: public ContrastEnhancement createContrastEnhancement(
710: Expression gammaValue) {
711: ContrastEnhancement ce = new ContrastEnhancementImpl();
712: ce.setGammaValue(gammaValue);
713:
714: return ce;
715: }
716:
717: public SelectedChannelType createSelectedChannelType(String name,
718: ContrastEnhancement enhancement) {
719: SelectedChannelType sct = new SelectedChannelTypeImpl();
720: sct.setChannelName(name);
721: sct.setContrastEnhancement(enhancement);
722:
723: return sct;
724: }
725:
726: public SelectedChannelType createSelectedChannelType(String name,
727: Expression gammaValue) {
728: SelectedChannelType sct = new SelectedChannelTypeImpl();
729: sct.setChannelName(name);
730: sct
731: .setContrastEnhancement(createContrastEnhancement(gammaValue));
732:
733: return sct;
734: }
735:
736: public StyledLayerDescriptor createStyledLayerDescriptor() {
737: return new StyledLayerDescriptorImpl();
738: }
739:
740: public UserLayer createUserLayer() {
741: return new UserLayerImpl();
742: }
743:
744: public NamedLayer createNamedLayer() {
745: return new NamedLayerImpl();
746: }
747:
748: public RemoteOWS createRemoteOWS(String service,
749: String onlineResource) {
750: RemoteOWSImpl remoteOWS = new RemoteOWSImpl();
751: remoteOWS.setService(service);
752: remoteOWS.setOnlineResource(onlineResource);
753:
754: return remoteOWS;
755: }
756:
757: public ShadedRelief createShadedRelief(Expression reliefFactor) {
758: ShadedRelief relief = new ShadedReliefImpl();
759: relief.setReliefFactor(reliefFactor);
760:
761: return relief;
762: }
763: }
|